dongzongzhi6953
2015-10-08 17:12
浏览 394
已采纳

'SyntaxError:意外的输入结束'。 解析JSON时出错?

I've got this AJAX function:

function fetchSocialCount(type,fileSrc){
    var count = null;
    var req = new XMLHttpRequest();
    req.onload = function(){
        if(req.status === 200 && req.readyState === 4){
            console.log(req.responseText);
            count = JSON.parse(req.responseText);
        }
    }
    req.open("GET","../scripts/php/returnSocialCount.php?type=" + type + "&fileSrc=" + fileSrc,false);
    req.send();
    if(count !== null){
        return count;
    }
    else{
        console.log("The AJAX request has failed.");
    }
}

But when I run it, I get a 'SyntaxError: Unexpected end of input'. error. The Chrome Dev Tools also covers the count = JSON.parse(req.responseText); part in blue, so I'm guessing it's something wrong with the JSON I'm recieving. From the PHP script, I'm sending a pretty complex JSON object, so I tried to send a really basic one and it worked without problems.

This is the part of the PHP script that sends the response:

echo '{"likes":'.$json->videoListArray[$i]->likes . ',"dislikes":' . $json->videoListArray[$i]->dislikes . '}';

Is there something wrong with the syntax of the echo? What's the problem?

Thanks.

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

我有这个AJAX函数:

  function fetchSocialCount  (type,fileSrc){
 var count = null; 
 var req = new XMLHttpRequest(); 
 req.onload = function(){
 if(req.status === 200&& req。  readyState === 4){
 console.log(req.responseText); 
 count = JSON.parse(req.responseText); 
} 
} 
 req.open(“GET”,“..  /scripts/php/returnSocialCount.php?type=“+ type +”& fileSrc =“+ fileSrc,false); 
 req.send(); 
 if(count!== null){
 return count  ; 
} 
其他{
 console.log(“AJAX请求失败。”); 
} 
} 
   
 
 

但是当我 运行它,我得到一个'SyntaxError:意外结束输入'。错误。 Chrome开发工具还包含 count = JSON.parse(req.responseText); 部分为蓝色,所以我猜测我收到的JSON有问题。 从PHP脚本,我发送一个非常复杂的JSON对象,所以我尝试发送一个非常基本的,它没有问题。

这是发送响应的PHP脚本的一部分:

  echo'{“likes”:'。$ json-  > videoListArray [$ i]  - >喜欢。  ',“不喜欢”:'。  $ json-> videoListArray [$ i]  - >不喜欢。  '}'; 
   
 
 

echo 的语法有问题吗? 有什么问题?

谢谢。

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • drnf09037160 2015-10-08 17:20
    已采纳

    It appears that there is probably a syntax error in your JSON that you're returning. It's not usually a good idea to manually create JSON for this very reason. Try creating an associative array and then use json_encode() to return the desired data to the ajax call:

    // Create the array
    $data = array();
    
    // Add the data
    $data['likes'] = $json->videoListArray[$i]->likes;
    $data['dislikes'] = $json->videoListArray[$i]->dislikes;
    
    // Generate JSON from the array and return the desired results
    echo json_encode($data);
    

    Reference Documentation:

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • duankan8739 2015-10-08 17:25

    You need to use json_encode (PHP json_encode manual page) and echo it like so:

    echo json_encode(array('likes'=>$json->videoListArray[$i]->likes, 'dislikes'=>$json->videoListArray[$i]->dislikes));
    

    My guess is you have a comma, semi-colon, or some other character in your response that is causing the issue. json_encode will make sure everything is formatted nicely.

    EDIT - War10ck beat me to the punch.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题