问题描述
我正在尝试调试以下jQuery.ajax()调用:
I am trying to debug the following jQuery.ajax() call:
var rollnum = $('#rollNum').val()
$.ajax({
url: sURL + "myController/myMethod",
type: "POST",
data: {rollnum: rollnum},
dataType: 'json',
success: function(json){alert(json)},
error:function (xhr, ajaxOptions, thrownError){
alert(xhr.status);
alert(thrownError);}
});
这是方法:
function myMethod(){
$query = $this->db->query("SELECT tblontario.Address, tblontario.Municipality FROM tblontario WHERE RollNum = 211010002528200");
$json_address = array();
$json_municipality = array();
foreach ($query->result_array() as $row){
$json_address[] = $row['Address'];
$json_municipality[] = $row['Municipality'];
}
$json['address'] = $json_address;
$json['municipality'] = $json_municipality;
echo json_encode($json);
exit;
}
当前正在发生的事情是我收到了一个错误.错误包含以下详细信息:
Currently what is happening is that I am receiving an error back. The error consists of these details:
xhr.status: 200; and
thrownError: SyntaxError: JSON.parse: unexpected character
当我在firebug中查看XHR Response选项卡时,它显示:
When I look at the XHR Response tab in firebug it shows:
Reload the page to get source for: http://www.mysite.ca/myController/myMethod
由于xhr.status为200,因此某些事情似乎正常运行,但是我似乎无法从此调用中检索到json数据.谁能给我关于问题可能出在哪里的建议?谢谢.
Something seems to be working correctly since we are getting the xhr.status of 200, but I can't seem to retrieve the json data back from this call. Can anyone give me a suggestion for what the problem might be? Thanks.
推荐答案
将内容的标头设置为json类型...这是设置标题类型的示例.
Set the header of the content to type of json...Here is an example of setting header type.
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');
从jQuery 1.4开始,严格按照JSON数据进行解析.
In jQuery 1.4 onwards the JSON data is parsed in a strict manner.
任何格式错误的JSON都会被拒绝,并引发解析错误.
Any malformed JSON is rejected and a parse error is thrown.
这篇关于jQuery.ajax()调用返回JSON.parse意外字符错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!