所以,本质上我有这个 Javascript 来解释我的 php 代码中的 JSON。它在本地服务器上运行得很好,但是,当我尝试将脚本移动到不同的服务器时,它将不起作用。我已经添加了
<?php header("Access-Control-Allow-Origin: *"); ?>
我还在 PHP 函数中将数据库连接声明为全局连接。
我很困惑为什么这些解决方案不起作用。另外,我知道有些脚本是不确定的,但我只想弄清楚为什么它在不同的服务器上不起作用。
<script type="text/javascript">
$("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;
});
});
</script>
当我从 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 代码相当长(我不想发布全部代码) - 但这里是缩短的版本:
<?php
header("Access-Control-Allow-Origin: *");
header('Content-Type: application/json');
require "../database/db.php";
if (is_ajax()) {
if (isset($_POST["action"]) && !empty($_POST["action"])) { //Checks if action value exists
$action = $_POST["action"];
switch($action) { //Switch case for value of action
case "test": test_function(); break;
}
}
}
//Function to check if the request is an AJAX request
function is_ajax() {
return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest';
}
function test_function(){
$c="1";
global $con;
$return = $_POST; //to reference post
$content=$return["content"];
//see if content is 140 characters or less
if(strlen($content)>140){ $c="0"; $lerror="<li>Your message must be 140 characters or less in length!</li>"; }
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'));
使用代码的这个简单版本,我可以成功发出跨域请求并控制台记录响应。如果您实现此代码但它仍然不起作用,则服务器和/或网络级别上有其他东西在起作用。
关于javascript - 获取json ajax php表单进行远程工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26008168/