获取json ajax php表单以远程工作

所以,基本上我有这个Javascript从我的PHP代码解释JSON。 它在本地服务器上运行良好,但是,当我尝试将脚本移动到其他服务器时,它将无法工作。 我添加了

 

我也在PHP函数中将我的数据库连接声明为全局。

我很困惑为什么这些解决方案不起作用。另外,我理解一些脚本是iffy,但我只是想知道为什么它不能从不同的服务器工作。

  $("document").ready(function(){ $(".sendText").submit(function(){ $("#sendButton").prop("disabled",true); $(".errors").html(""); $(".success").html(""); var data = { "action": "test" }; data = $(this).serialize() + "&" + $.param(data); $.ajax({ type: "POST", dataType: "jsonp", //new edit url: "http://myurl.com/testing/jsonpost.php?callback=test", //new edit data: data, success: function(data) { if(data["success"]=="yes") { $(".success").html("Message Sent!"); $(".formContainer").html("" + data["json"] + ""); } else { if(document.getElementById("sendButton").disabled = true){ document.getElementById("sendButton").disabled = false; } $(".errors").html("" + data["errors"] + ""); } } }); return false; }); });  

当我从firebug查看Web控制台时的一些信息:

 Access-Control-Allow-Orig... * Connection Keep-Alive Content-Length 0 Content-Type application/json Date Wed, 24 Sep 2014 04:22:57 GMT Keep-Alive timeout=5, max=100 Server Apache/2.2.27 (Unix) mod_ssl/2.2.27 OpenSSL/1.0.1e-fips DAV/2 mod_bwlimited/1.4 X-Powered-By PHP/5.4.29 

看起来它正在与服务器通信但无法解释数据? 想法?

此外,此错误在远程服务器的控制台中出现,但在本地服务器上运行时则不会出现:

 SyntaxError {stack: (...), message: "Unexpected end of input"}message: "Unexpected end of input"stack: (...) Object {readyState: 4, getResponseHeader: function, getAllResponseHeaders: function, setRequestHeader: function, overrideMimeType: function… parsererror 

PHP代码很长(我不想发布所有代码) – 但这里是缩短版本:

 140){ $c="0"; $lerror="
  • Your message must be 140 characters or less in length!
  • "; } if($c=="0"){ //doesnt pass validation $return["success"]="no"; $return["errors"]="$lerror"; } if($c!="0"){ //passes validation $return["success"]="yes"; } if(isset($_GET['callback'])){ //jsonp edit $return["json"] = json_encode($return); echo $_GET['callback']."(".json_encode($return).")"; //jsonp edit } }

    在远程服务器上转换为JSONP后 – 获取错误 –

     "jQuery111006159528985153884_1411663761720 was not called" 

    使用JSON数据类型处理jQuery AJAX时,服务器端脚本产生的任何通知,警告或错误都会导致问题。 原因是输出的PHP错误打破了jQuery期望的JSON编码。

    我怀疑这两个环境不完全相同,可能是不同的PHP版本,缺少PHP扩展或php.ini文件中的不同设置。

    最好的办法是使用提供的jQuery AJAX错误回调来控制日志记录任何错误,从而基本上解决服务器端脚本抛出的任何问题。

    ! 编辑3 !!!

    客户代码

     $.ajax({ type: "POST", dataType: "json", url: "http://myurl.com/jsonpost.php", data: data, success: function(response) { console.log(response); }, error: function(xhr, status, error) { console.log(xhr); console.log(status); console.log(error); } }); 

    服务器端代码

     header("Access-Control-Allow-Origin: *"); header('Content-Type: application/json'); echo json_encode(array('success' => 'yes')); 

    使用您的代码的这个简单版本我可以成功地发出跨域请求和控制台记录响应。 如果您实现此代码并且它仍然无法正常工作,那么服务器和/或网络级别还有其他function。

    您可以对后端API进行AJAX调用,它需要返回JSONP格式而不仅仅是JSON,否则您可能会收到错误。 这是由于相同的原产地政策:

    https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy

    这个讨论可能有助于理解JSONP:

    用外行人的话可以解释一下JSONP是什么吗?

    但是,一种替代方法是禁用Google Chrome浏览器的安全性,然后它就能正常运行。 但这不是解决方案。 最好使用JSonP格式。