doumou8527
2015-01-08 04:46
浏览 340

JSon解析我的代码返回错误

<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.

图片转代码服务由CSDN问答提供 功能建议

 &lt; script type =“text / javascript”&gt; 
 $(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 
}); 
}); 
&lt; / script&gt; 
   
 
 

上面是我的代码。 2.php的返回结果是

  {“name”:“Halford Tee”,“type”:“client”,“mobile”:“1234567”,“location”  “:”“,”“description”:“”} 
   
 
 

它给我一个 错误

 未捕获的SyntaxError:意外的输入结束
   
 
 

当我删除此部分时 < pre> var obj = JSON.parse(dataFetch); alert(obj [0] .name);

错误消失了。 我的代码有什么问题吗?


查看同行程序员的宝贵指南 < p>我的2.php返回

  {“name”:“David Tan”,“type”:“leads”,“mobile”:“91234567”,“location”  :“”,“description”:“”} 
   
 
 

我尝试了以下

  $(document)  .ready(function(){
var obj =“”; 
var param = [{“name”:“eventId”,value:9}]; 
 $ .ajax({
 type:'POST',\  n url:'2.php',
 data:param,
 success:function(dataFetch){
alert(dataFetch [0] .name); 
},
 dataType:'json',
 async  :false 
}); 
}); 
   
 
 

它给我一个错误

 无法读取 属性'name'未定义。
   
 
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • douyimiao1993 2015-01-08 05:11
    已采纳

    when you use dataType: 'json' in your ajax then you don't need to use JSON.parse. your data will automatically be parsed in json format.so you can directly use it.

     use : `alert(dataFetch.name);`// you do not have any array so directly use it.your data is an object not array of object.
    

    note: i think you should use async:false only when your calling has to complete before the next statement in your function can be called.

    打赏 评论
  • duande1985 2015-01-08 05:18

    After: var obj = JSON.parse(dataFetch);

    alert(JSON.stringify(obj));
    

    it might help you to rectify your problem.

    打赏 评论
  • dsla94915 2015-01-08 06:37

    (This answer is based on your comment that there might be more than one row.)

    I think this is one of those always-1 vs 0-or-more vs 1-or-more problems.

    You have to decide, exactly what does 2.php return?

    1. Always a single row? {"name":"David Tan",...}
    2. Always an array of zero-or-more rows? [{"name":"David Tan",...}, {"name":"Clark Kent",...},...]
    3. Or simply one-or-more rows, but not bracketed as an array? {"name":"David Tan",...}{"name":"Clark Kent",...}

    In case (1), dataFetch.name is "David Tan".

    In case (2), dataFetch[0].name is "David Tan".

    In case (3), your output is not valid JSON. If you have the ability to change the PHP script, then change the output to Case (2) format <<GOOD SOLUTION. If you must deal with someone else's PHP script, then you need to change your ajax call to dataType: "text", then clean it up to make it valid JSON, then parse the JSON client-side. You do not want to omit dataType and have jQuery default to "Intelligent Guess", because results will vary depending on how many rows there are in the output.

    打赏 评论

相关推荐 更多相似问题