jquery无法读取json响应(使用$.post)
我正在提交一个数据类型为json的post请求。 我能够在fiddrel中看到json响应,但jquery无法解析它 这是我的密码:jquery无法读取json响应(使用$.post),jquery,json,http-post,Jquery,Json,Http Post,我正在提交一个数据类型为json的post请求。 我能够在fiddrel中看到json响应,但jquery无法解析它 这是我的密码: $("#jsonTestCasePost").click(function(){ var requestType = $("#requestType").val(); $('#result').val(requestType); debugger; $.post("http://localhost/a
$("#jsonTestCasePost").click(function(){
var requestType = $("#requestType").val();
$('#result').val(requestType);
debugger;
$.post("http://localhost/api/number/substract", {numberA:"32",numberB:"10"},
function(data){
debugger;
$('#result').val(data);
}, requestType);
});
这是我在fiddler中的原始响应文本
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 15
Server: Jetty(6.1.11)
{"result":"22"}
在jquery done函数中,我看到以下值:
status: 0
statusTex: "",
responses: {}
headers: ""
知道我做错了什么吗?
谢谢您需要使用数据的结果属性:
$('#result').val(data.result);
您需要使用数据的结果属性:
$('#result').val(data.result);
在执行跨浏览器请求时,请使用jquery的ajax
$.ajax({
url:'http://localhost/api/number/substract',
type:'POST',
data:{numberA:"32",numberB:"10"},
dataType:'json',
async:false,
cache:false,
crossDomain:true,
success:function(data){
$('#result').val(data.result);
},
error:function(jxhr){
console.log(jxhr.responseText);
}
});
在执行跨浏览器请求时,请使用jquery的ajax
$.ajax({
url:'http://localhost/api/number/substract',
type:'POST',
data:{numberA:"32",numberB:"10"},
dataType:'json',
async:false,
cache:false,
crossDomain:true,
success:function(data){
$('#result').val(data.result);
},
error:function(jxhr){
console.log(jxhr.responseText);
}
});
从同一台服务器发出请求解决了我的问题。感谢gilly3的评论。从同一台服务器发出请求解决了我的问题。感谢gilly3的评论。根据要求,以下是我的评论,以回答的形式:
您很可能是由于以下原因而受到限制的受害者。确保将您的请求发送到页面所在的服务器。例如,如果您请求http://localhost/api/number/substract
,您当前的请求页面必须位于http://localhost
根据要求,以下是我的回答:
您很可能是由于以下原因而受到限制的受害者。确保将您的请求发送到页面所在的服务器。例如,如果您请求http://localhost/api/number/substract
,您当前的请求页面必须位于http://localhost
var requestType
包含哪些内容json
?我正在创建一个测试页面,允许在其中选择json或XML。您是否向页面所在的服务器发出请求?例如,如果您请求http://localhost/api/number/substract
,是您当前在http://localhost
?不,我没有。在本地主机下移动我的测试页面后,我能够看到正确的响应。谢谢你介意把你的评论作为回答吗?我整个上午都在调试这个。谢谢。var requestType
包含什么json
?我正在创建一个测试页面,允许在其中选择json或XML。您是否向页面所在的服务器发出请求?例如,如果您请求http://localhost/api/number/substract
,是您当前在http://localhost
?不,我没有。在本地主机下移动我的测试页面后,我能够看到正确的响应。谢谢你介意把你的评论作为回答吗?我整个上午都在调试这个。谢谢。我认为jquery的$.post最后使用了$.ajax。。是吗?post
是一个速记Ajax函数,请看这里,但是$.Ajax
提供了更多选项,如crossDomain
etcI think jquery的$。post最后使用$.Ajax。。是吗?post
是一个简写的Ajax函数,请看这里,但是$。Ajax
提供了更多选项,如跨域等