<script type="text/javascript">
$(document).ready(function(){
var obj = "";
var param = [{ "name": "id", value: 9}];
$.ajax({
type: 'POST',
url: '2.php',
data: param,
success: function(dataFetch) {
var obj = JSON.parse(dataFetch);
alert(obj[0].name);
},
dataType: 'json',
async:false
});
});
</script>
above is my code. The return result of 2.php is
{"name":"Halford Tee","type":"client","mobile":"1234567","location":"","description":""}
It give me an error
Uncaught SyntaxError: Unexpected end of input
When I remove this part
var obj = JSON.parse(dataFetch);
alert(obj[0].name);
The error is gone. Is there anything wrong with my code?
After looking at valuable guide from fellow programer
My 2.php returns
{"name":"David Tan","type":"leads","mobile":"91234567","location":"","description":""}
I tried the following
$(document).ready(function(){
var obj = "";
var param = [{ "name": "eventId", value: 9}];
$.ajax({
type: 'POST',
url: '2.php',
data: param,
success: function(dataFetch) {
alert(dataFetch[0].name);
},
dataType: 'json',
async:false
});
});
It give me an error
cannot read property 'name' of undefined.