Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jquery无法读取json响应(使用$.post)_Jquery_Json_Http Post - Fatal编程技术网

jquery无法读取json响应(使用$.post)

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

我正在提交一个数据类型为json的post请求。 我能够在fiddrel中看到json响应,但jquery无法解析它

这是我的密码:

$("#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
提供了更多选项,如
跨域