I am trying to create a simple AJAX call using JSON, but I keep getting a parse error on the result and when I check the resultText it shows the source code of the entire page, with the JSON response showing a success above the page headers.
我正在尝试使用JSON创建一个简单的AJAX调用,但我一直在结果上得到一个解析错误,当我检查resultText时,它显示整个页面的源代码,JSON响应在页面标题上方显示成功。
Here is my AJAX call, where #class
is a select box with values.
这是我的AJAX调用,其中#class是一个带值的选择框。
$("#class").change( function () {
if($('#class').val().length > 0){
$.ajax({
type: "GET",
url: "http://192.168.0.9/ajax",
data: 'class_id=' + $("#class").val(),
datatype: 'json',
async: 'true',
beforeSend: function() {
alert("Before send!");
},
success: function(result){
if (result.status){
$('#result').html(result);
} else {
alert('request unsuccessful!');
}
},
complete: function() {
alert("Complete!");
},
error: function (request,error) {
alert('A jQuery error has occurred. Status: ' + error +':'+ request.responseText);
$("#result").html(request.responseText);
}
});
} else {
alert('Please make a selection');
}
return false;
});
This is my PHP function that returns the result
这是我的PHP函数,它返回结果
$result = array();
$result['status'] = "success";
$result['message'] = "Types";
header("Content-Type: application/json; charset=utf-8", true);
echo json_encode($result);
Finally, this is the response I am getting in my error alert:
最后,这是我在错误提醒中得到的响应:
A jQuery error has occurred status:parseerror
{"status":"success", "message":"Types"}<!DOCTYPE html>
<html>
...rest of my page from where the request was sent
I am hoping this is a simple error and someone can tell me what I am doing wrong?
我希望这是一个简单的错误,有人可以告诉我我做错了什么?
2 个解决方案
#1
0
Perhaps your parameter should be pass in JSON format:
也许您的参数应该以JSON格式传递:
data: "{'class_id':'" + $("#class").val() + "'}",
#2
0
Try to remove datatype:'json'
from the Javascript and header("Content-Type: application/json; charset=utf-8", true);
it should be recognize itself
尝试从Javascript和标题中删除数据类型:'json'(“Content-Type:application / json; charset = utf-8”,true);它应该认识到自己
#1
0
Perhaps your parameter should be pass in JSON format:
也许您的参数应该以JSON格式传递:
data: "{'class_id':'" + $("#class").val() + "'}",
#2
0
Try to remove datatype:'json'
from the Javascript and header("Content-Type: application/json; charset=utf-8", true);
it should be recognize itself
尝试从Javascript和标题中删除数据类型:'json'(“Content-Type:application / json; charset = utf-8”,true);它应该认识到自己