【问题标题】:Jquery Callback Was Not Called未调用 Jquery 回调
【发布时间】:2014-05-30 12:44:05
【问题描述】:

我已经在 Google 上搜索了我能找到的所有此错误实例,但没有一个解决方案适合我。本质上,我正在做其他人建议的事情。尽管如此,我收到一个错误,即我的回调函数没有被调用。我正在使用 Ajax/Jquery/JASONP 使用 GET 进行跨域表单提交。这是我在表单方面的代码:

$(document).ready(function() {
$("#ajaxform1").submit(function(e){
e.preventDefault();
var surl = "http://blahblah/test_create_user.php?callback=?";

            $.ajax({
                type: 'GET',
                url: surl,
            crossDomain: true,
                data:  $('#ajaxform1').serialize(),
                dataType: "jsonp",
            success: function(msg) {

            $.each(msg, function (index, value) {
             if(value==1)
              {
            alert("New User Added");
                  }   else {
              alert("User Already Exists")
                   }                     }
             });

                },
                error: function(xhr, status, error) {
            var myerror = xhr+" "+status+" "+error;
            alert("Failure Connecting to Kayako. Please Try    Again("+myerror+")"); }

            });

这是我的 PHP 代码的适用 sn-p:

if($USER_CHK->first()->id){
  $data = array('msg' => '0'); // user exists
  else {
            $data = array('msg' => '1'); // User added
       }
    //echo customerAdded(FALSE);
    header('Access-Control-Allow-Origin: '.$_SERVER['HTTP_ORIGIN']);
    header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
    header('Access-Control-Max-Age: 1000');
    header('Access-Control-Allow-Headers: Content-Type, Authorization,X-   Requested-      With');
    header("Content-type: application/json");  
    $data = array('msg' => '0');
    print $_REQUEST['callback']. '('.json_encode($data).')'; 
    exit;

      });           
            });

我的调试显示所有表单变量都已发布。 PHP 代码返回:jQuery11020900643879813015_1397599599587({"msg":"1"}) 然而,表明回调未被调用的错误被抛出。

【问题讨论】:

  • 我不确定,但回复 content-type 应该是 application/x-javascript
  • 谢谢Shakib。我改了,还是没有奖品。

标签: php jquery ajax jsonp


【解决方案1】:

请求在服务器端是什么样子的?

我有一个与您所拥有的非常相似的工作示例。唯一的区别是我回复的内容类型是:

'text/javascript'

另外,我的 JSONP 消息末尾包含一个分号(我认为这并不是真正的失败原因)。

returnJSONP = $_REQUEST['callback']. '('.json_encode($data).');'; 

如果您能解决,能否请您发布解决方案?

【讨论】:

    【解决方案2】:

    我放弃了尝试让它工作,而是发布到本地 php 脚本,然后使用 CURL 发布到远程服务器。我将 CURL 结果回显到 jQuery 函数,一切正常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-02-05
      • 2011-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-01
      相关资源
      最近更新 更多