我正在处理一些代码示例,但我遇到了一些问题,无法做我想要的事情。
这是一个代码示例。我在互联网上发现了它的一部分,并尝试使用它。
在上面的情况下它可以很好地工作,但是当目标URL不同时它就会完成
在第一个例子中,目标提供了json。 在第二个例子中,目标提供了jsonp。
不同之处在于,对于第二个示例,我将json设置为'true'值。 我真的不明白为什么它不起作用。
如果有人可以解释我的原因'我尝试了很多我在互联网上找到的东西,但没有真正有效。
非常感谢那些花时间解决我的问题并帮助我找出问题所在的人;)
样本1:
<!doctype html>
<html>
<head>
<title>JSONP example</title>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
</head>
<body>
<h1>Test</h1>
<script>
$.ajax({
type: 'GET',
dataType: "json",
processData: false,
crossDomain: true,
jsonp: false,
url: "http://flxn.eu/json.php",
success: function (responseData, textStatus, jqXHR)
{
console.debug(responseData);
$.each(responseData, function (index, value) {
console.debug(value);
$('body').append(value.name + '<br />' + value.address + '<br />' + value.city + '<br />' + value.postcode + '<br />' + '<br />');
});
},
error: function (responseData, textStatus, errorThrown) {
alert('POST failed.');
}
});
</script>
</body>
</html>
样本2:
<!doctype html>
<html>
<head>
<title>JSONP example</title>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
</head>
<body>
<h1>test jsonP</h1>
<script>
$.ajax({
type: 'GET',
dataType: "json",
processData: false,
crossDomain: true,
jsonp: true,
url: "http://widget.mondialrelay.com//parcelshop-picker/v3_0/services/parcelshop-picker.svc/SearchPR?Brand=BDTEST%20%20&Country=FR&PostCode=62620&ColLivMod=24R&Weight=&NbResults=7&SearchDelay=&SearchFar=75&=Zone_Widget&VacationBefore=&VacationAfter=&Service=&Latitude=&Longitude=&method=jQuery16206304910685867071_1380876031038&_=1380879686732",
success: function (responseData, textStatus, jqXHR)
{
console.debug(responseData);
},
error: function (responseData, textStatus, errorThrown) {
alert('POST failed.');
}
});
</script>
</body>
</html>
答案 0 :(得分:1)
您需要告诉jQuery将JSONP回调名称放在何处。
将网址参数更改为&method=?
。
答案 1 :(得分:0)
以下是jsonp跨域
的工作示例这就是你要找的东西吗?
如果您已请求查询字符串
?callback=my_callback_method
然后,您的服务器必须响应包含这样的数据:
my_callback_method({your json serialized data});
请参阅:Make cross-domain ajax JSONP request with jQuery
希望如果你的json没问题,这将有效。
<!doctype html>
<html>
<head>
<title>JSONP example</title>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
</head>
<body>
<h1>test jsonP</h1>
<script>
var url = 'http://widget.mondialrelay.com//parcelshop-picker/v3_0/services/parcelshop-picker.svc/SearchPR?Brand=BDTEST%20%20&Country=FR&PostCode=62620&ColLivMod=24R&Weight=&NbResults=7&SearchDelay=&SearchFar=75&=Zone_Widget&VacationBefore=&VacationAfter=&Service=&Latitude=&Longitude=&method=jQuery16206304910685867071_1380876031038&_=1380879686732?callback=?';
$.ajax({
type: 'GET',
url: url,
async: false,
jsonpCallback: 'jQuery16206304910685867071_1380876031038',
contentType: "application/json",
dataType: 'jsonp',
success: function(json) {
console.dir(json.PRList);
},
error: function(e) {
console.log(e.message);
}
});
</script>
</body>
</html>