简体   繁体   English

使用jQuery / AJAX与JSONP时没有响应

[英]No response when using jQuery/AJAX with JSONP

I am trying to make a cross-domain request using jQuery/AJAX. 我正在尝试使用jQuery / AJAX创建跨域请求。 I have the following code; 我有以下代码;

$.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 (ie through dynamic script tag injection) URL http://www.cjihrig.com/development/jsonp/jsonp.php?callback=jsonpCallback&message=Hello在直接调用时返回JSON对象,并且在以传统方式使用JSONP时工作正常(即通过动态脚本标记注入)

But why do I get an error when using it with jQuery/AJAX ? 但是为什么我在使用jQuery / AJAX时遇到错误?

Try this code because the error isn't set the dataType and isn't expect a jsonp default 尝试此代码,因为错误未设置dataType,并且不期望jsonp默认
dataType: (default: Intelligent Guess (xml, json, script, or html)) dataType :(默认值:Intelligent Guess(xml,json,script或html))
Type: 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");
    }

DEMO DEMO

I would use $.ajax with the option: 我会使用$ .ajax选项:

dataType: "jsonp"

This automatcially adds the callback option to the url. 这会自动将回调选项添加到URL。 http://api.jquery.com/jQuery.ajax/ http://api.jquery.com/jQuery.ajax/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM