I am trying to make a cross-domain request using jQuery/AJAX. I have the following code;
我正在尝试使用jQuery/AJAX发出跨域请求。我有以下代码;
$.ajax({
url: "http://www.cjihrig.com/development/jsonp/jsonp.php?callback=jsonpCallback&message=Hello",
crossDomain:true
})
.done(function( msg ) {
alert( "Done : " + msg );
})
.fail(function( msg) {
alert( "Fail : " + msg);
})
.always(function( msg ) {
alert( "Always : " + msg );
});
The URL http://www.cjihrig.com/development/jsonp/jsonp.php?callback=jsonpCallback&message=Hello returns JSON object when calling directly and works fine when using JSONP in traditional manner (i.e. through dynamic script tag injection)
URL http://www.cjihrig.com/development/jsonp/jsonp.php?当在传统的方式(例如通过动态脚本标记注入)使用JSONP时直接调用和工作时,Hello将返回JSON对象。
But why do I get an error when using it with jQuery/AJAX ?
但是为什么在使用jQuery/AJAX时会出现错误呢?
2 个解决方案
#1
1
Try this code because the error isn't set the dataType and isn't expect a jsonp default
dataType: (default: Intelligent Guess (xml, json, script, or html))
Type: String
尝试这段代码,因为错误没有设置数据类型,并且不期望jsonp默认数据类型:(默认:智能猜测(xml、json、脚本或html)类型:String
$.ajax({
url: "http://www.cjihrig.com/development/jsonp/jsonp.php?callback=jsonpCallback&message=Hello",
dataType: 'jsonp',
crossDomain:true,
jsonp: false,
success: jsonpCallback,
})
.done(function( msg ) {
alert( "Done : " + msg );
})
.fail(function( msg) {
alert( "Fail : " + msg);
})
.always(function( msg ) {
alert( "Always : " + msg );
});
function jsonpCallback(data){
alert("jsonpCallback");
}
演示
#2
0
I would use $.ajax with the option:
我将使用美元。ajax选项:
dataType: "jsonp"
This automatcially adds the callback option to the url. http://api.jquery.com/jQuery.ajax/
这个自动向url添加回调选项。http://api.jquery.com/jQuery.ajax/
#1
1
Try this code because the error isn't set the dataType and isn't expect a jsonp default
dataType: (default: Intelligent Guess (xml, json, script, or html))
Type: String
尝试这段代码,因为错误没有设置数据类型,并且不期望jsonp默认数据类型:(默认:智能猜测(xml、json、脚本或html)类型:String
$.ajax({
url: "http://www.cjihrig.com/development/jsonp/jsonp.php?callback=jsonpCallback&message=Hello",
dataType: 'jsonp',
crossDomain:true,
jsonp: false,
success: jsonpCallback,
})
.done(function( msg ) {
alert( "Done : " + msg );
})
.fail(function( msg) {
alert( "Fail : " + msg);
})
.always(function( msg ) {
alert( "Always : " + msg );
});
function jsonpCallback(data){
alert("jsonpCallback");
}
演示
#2
0
I would use $.ajax with the option:
我将使用美元。ajax选项:
dataType: "jsonp"
This automatcially adds the callback option to the url. http://api.jquery.com/jQuery.ajax/
这个自动向url添加回调选项。http://api.jquery.com/jQuery.ajax/