Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.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的Ajax:200可以,但不是“;成功”;_Jquery_Json_Cross Domain_Jsonp - Fatal编程技术网

带JQuery的Ajax:200可以,但不是“;成功”;

带JQuery的Ajax:200可以,但不是“;成功”;,jquery,json,cross-domain,jsonp,Jquery,Json,Cross Domain,Jsonp,我在尝试跨域请求时也面临同样的问题。情况很奇怪,我的数据是在浏览器上直接点击请求的url时加载的,奇怪的是,如果使用jquery ajax请求,也会加载数据, 但不是在firebug控制台上,而是在firebug网络选项卡上。 控制台打印: 错误数据示例:parsererror{虽然json数据格式良好,但在json验证器上检查} readyState: 4 status: 200 responseText: undefined Net选项卡加载response和json子选项卡中的所有数据

我在尝试跨域请求时也面临同样的问题。情况很奇怪,我的数据是在浏览器上直接点击请求的url时加载的,奇怪的是,如果使用jquery ajax请求,也会加载数据, 但不是在firebug控制台上,而是在firebug网络选项卡上。 控制台打印: 错误数据示例:parsererror{虽然json数据格式良好,但在json验证器上检查}

readyState: 4 status: 200 responseText: undefined
Net选项卡加载responsejson子选项卡中的所有数据

我的示例代码是:

function fetchJsonData() {
$.ajax({
    type: 'POST',
    url: 'http://www.meilleurmobile.com/comparateur/resultats-comparateur-json.do',
    data: 'monthDur%5B0%5D=45.75&monthDur%5B1%5D=45.75&monthDur%5B2%5D=45.75&monthDur%5B3%5D=45.75&monthDur%5B4%5D=45.75&monthDur%5B5%5D=45.75&monthDur%5B6%5D=45.75&monthDur%5B7%5D=45.75&monthDur%5B8%5D=45.75&monthDur%5B9%5D=45.75&monthDur%5B10%5D=45.75&monthDur%5B11%5D=45.75&numSms=1000&dataVolume=1000&withoutMobile=-1&commitmentDuration=-1',
    async: false,
    cache: false,
    //contentType: 'application/json; charset=utf-8',
    crossDomain: true,
    dataType: 'jsonp',
    error: function( xhr,err ) {
        console.log( 'Sample of error data:', err );
        console.log("readyState: "+xhr.readyState+"\nstatus: "+xhr.status+"\nresponseText: "+xhr.responseText);
    },
    success: function( data ) {
        if (console && console.log) {
            console.log( 'Sample of data:', data.slice(0,100) );
        }
    }
})
.done(function() { alert("success"); })
.fail(function() { alert("error"); });
}

尝试使用

 console.log(data);
没有切片

或者,甚至更好

 console.log(JSON.stringify(data.slice(0,100)));
使用JSON.stringify()(文档化)方法,您会得到一个字符串,该字符串不会被解析或检查JSON的有效性

希望这能有所帮助。

试试看

 console.log(data);
没有切片

或者,甚至更好

 console.log(JSON.stringify(data.slice(0,100)));
使用JSON.stringify()(文档化)方法,您会得到一个字符串,该字符串不会被解析或检查JSON的有效性


希望这有帮助。

尝试跨域XMLHttpRequest请求可能会愚弄您。在Firefox web控制台中,它看起来像加载了URL,但正文是一个空字符串

确认服务器支持JsonP。如果你真的不知道这意味着什么,你需要查一下。这是至关重要的

jQuery假定JsonP参数将是“”callback=“。如果不是这样,您应该看到:

jsonp:重写jsonp请求中的回调函数名。此值 将在函数的“callback=?”部分中使用而不是“callback” url中的查询字符串。所以{jsonp:'onJSONPLoad'}将导致 “onJSONPLoad=?”已传递给服务器。从jQuery1.5开始,设置 jsonp选项设置为false可防止jQuery添加“”回调 字符串或尝试使用“=?”进行转换。在里面 在这种情况下,还应该显式设置jsonpCallback设置。 例如,{jsonp:false,jsonpCallback:“callbackName”}

jsonpCallback:指定JSONP请求的回调函数名。此值 将使用,而不是由自动生成的随机名称 jQuery。最好让jQuery生成一个唯一的名称,因为它将 使管理请求和提供回调和错误信息变得更容易 处理。您可能希望在要启用时指定回调 更好的GET请求浏览器缓存。从jQuery1.5开始,您还可以 使用此设置的函数,在这种情况下 jsonpCallback被设置为该函数的返回值

如果它变得混乱,可能更容易使用老式的方式,并在URL中使用时间戳将脚本附加到页面中,以避免使用缓存的脚本页面


顺便说一句,好吧,没有办法把JSONP和POST结合起来。JSONP是一种围绕XMLHttpRequest的同源安全策略工作的方法。它要求您向DOM追加一个脚本。我认为您不能这样做,也不能将POST变量作为过程的一部分提交。

尝试跨域XMLHttpRequest请求可能会愚弄您。在Firefox web控制台中,它看起来像加载了URL,但正文是一个空字符串

function fetchJsonData() {
$.ajax({
    type: 'POST',
    url: "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20json%20where%20url%20%3D%20'www.meilleurmobile.com%2Fcomparateur%2Fresultats-comparateur-json.do%3FmonthDur%255B0%255D%3D45.75%26monthDur%255B1%255D%3D45.75%26monthDur%255B2%255D%3D45.75%26monthDur%255B3%255D%3D45.75%26monthDur%255B4%255D%3D45.75%26monthDur%255B5%255D%3D45.75%26monthDur%255B6%255D%3D45.75%26monthDur%255B7%255D%3D45.75%26monthDur%255B8%255D%3D45.75%26monthDur%255B9%255D%3D45.75%26monthDur%255B10%255D%3D45.75%26monthDur%255B11%255D%3D45.75%26numSms%3D1000%26dataVolume%3D1000%26withoutMobile%3D-1%26commitmentDuration%3D-1%26_%3D1329825461536'&format=json&diagnostics=true",
    async: false,
    cache: false,
    crossDomain: true,
    dataType: 'jsonp',
    error: function( xhr,err ) {
        console.log( 'Sample of error data:', err );
        console.log("readyState: "+xhr.readyState+"\nstatus: "+xhr.status+"\nresponseText: "+xhr.responseText);
    },
    success: function( data, status ) {
        if (console && console.log) {
            console.log( 'data count:', data.query.results.json.json.length );
            $('#result-count').text( JSON.stringify(data.query.results.json.json) );
        }
    },
    jsonpCallback: 'swatCallback'
})
.done(function() { alert("success"); })
.fail(function() { alert("error"); }); 
}


window['swatCallback'] = function(data) {
    var info = data.query.results.json.json;
    $('#callback_result_operator').html(info[0].operator);
};
确认服务器支持JsonP。如果你真的不知道这意味着什么,你需要查一下。这是至关重要的

jQuery假定JsonP参数将是“”callback=“。如果不是这样,您应该看到:

jsonp:重写jsonp请求中的回调函数名。此值 将在函数的“callback=?”部分中使用而不是“callback” url中的查询字符串。所以{jsonp:'onJSONPLoad'}将导致 “onJSONPLoad=?”已传递给服务器。从jQuery1.5开始,设置 jsonp选项设置为false可防止jQuery添加“”回调 字符串或尝试使用“=?”进行转换。在里面 在这种情况下,还应该显式设置jsonpCallback设置。 例如,{jsonp:false,jsonpCallback:“callbackName”}

jsonpCallback:指定JSONP请求的回调函数名。此值 将使用,而不是由自动生成的随机名称 jQuery。最好让jQuery生成一个唯一的名称,因为它将 使管理请求和提供回调和错误信息变得更容易 处理。您可能希望在要启用时指定回调 更好的GET请求浏览器缓存。从jQuery1.5开始,您还可以 使用此设置的函数,在这种情况下 jsonpCallback被设置为该函数的返回值

如果它变得混乱,可能更容易使用老式的方式,并在URL中使用时间戳将脚本附加到页面中,以避免使用缓存的脚本页面


顺便说一句,好吧,没有办法把JSONP和POST结合起来。JSONP是一种围绕XMLHttpRequest的同源安全策略工作的方法。它要求您向DOM追加一个脚本。我认为您不能这样做,也不能将POST变量作为流程的一部分提交。

该服务似乎不支持JSONP,它只返回JSON()。JOSNP不是魔术。这个URL不会返回JSONP。如果我使用JSON,控制台会说200 OK,并带有红色的失败标记,即使成功了。最重要的是,它发生在错误块上,而不是成功。在使用JSONP时,控制台说没有操作,但Firebug的Net选项卡说数据加载在正确的JSON对象中,这根本不反映在控制台中。该服务似乎不支持JSONP,它只返回JSON()。JOSNP不是魔术。这个URL不会返回JSONP。如果我使用JSON,控制台会说200 OK,并带有红色的失败标记,即使成功了。最重要的是,它发生在错误块上,而不是成功。在使用JSONP时,控制台说没有操作,但Firebug的Net选项卡说数据加载到了正确的JSON对象中,这在控制台中根本没有反映出来。Daniele,它没有成功,但出现了错误块。
function fetchJsonData() {
$.ajax({
    type: 'POST',
    url: "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20json%20where%20url%20%3D%20'www.meilleurmobile.com%2Fcomparateur%2Fresultats-comparateur-json.do%3FmonthDur%255B0%255D%3D45.75%26monthDur%255B1%255D%3D45.75%26monthDur%255B2%255D%3D45.75%26monthDur%255B3%255D%3D45.75%26monthDur%255B4%255D%3D45.75%26monthDur%255B5%255D%3D45.75%26monthDur%255B6%255D%3D45.75%26monthDur%255B7%255D%3D45.75%26monthDur%255B8%255D%3D45.75%26monthDur%255B9%255D%3D45.75%26monthDur%255B10%255D%3D45.75%26monthDur%255B11%255D%3D45.75%26numSms%3D1000%26dataVolume%3D1000%26withoutMobile%3D-1%26commitmentDuration%3D-1%26_%3D1329825461536'&format=json&diagnostics=true",
    async: false,
    cache: false,
    crossDomain: true,
    dataType: 'jsonp',
    error: function( xhr,err ) {
        console.log( 'Sample of error data:', err );
        console.log("readyState: "+xhr.readyState+"\nstatus: "+xhr.status+"\nresponseText: "+xhr.responseText);
    },
    success: function( data, status ) {
        if (console && console.log) {
            console.log( 'data count:', data.query.results.json.json.length );
            $('#result-count').text( JSON.stringify(data.query.results.json.json) );
        }
    },
    jsonpCallback: 'swatCallback'
})
.done(function() { alert("success"); })
.fail(function() { alert("error"); }); 
}


window['swatCallback'] = function(data) {
    var info = data.query.results.json.json;
    $('#callback_result_operator').html(info[0].operator);
};