jQuery Ajax到PHP MySQL – 跨域内部服务器Erro(500)

我有一个jQuery ajax调用使用PHP脚本更新我的数据库。

这是我的电话:

$.ajax({ url: "update.php", type: 'POST', dataType: 'jsonp', data: {key1: value1, key2: value2}, cache: false, error: function() { $("#failUpload").removeClass("hide"); }, success: function(data) { $("#succesUpload").removeClass("hide"); setTimeout(function() { $("#succesUpload").addClass("hide"); }, 5000); } }); 

PHP更新部分:

 $key1 = $_POST["key1"]; $key2 = $_POST["key2"]; $con=mysqli_connect("localhost","username","password","dbname"); if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } $sql = "UPDATE TabelName SET ". $key2 ." ='". $key1 ."' WHERE id=1"; if ($result = mysqli_query($con, $sql)) { $resultArray = array(); $tempArray = array(); while ($row = $result->fetch_object()) { $tempArray = $row; array_push($resultArray, $tempArray); } } mysqli_close($con); 

数据库更新,它的工作原理,但在console.log中我收到此错误消息: POST http://domainname.com/file.php?callback=jQuery2110765103816287592_1432976576289 500 (Internal Server Error)当我打开它时,我发现:

 _.ajaxTransport.Y.cors.a.crossDomain.send @ jquery.js:26 

我已经搜索过并发现了Cross Domain调用的东西,你必须使用jsonp等,但它没有用。 谢谢!

使用以下function进行错误。 它将显示确切的问题。 我认为这会有所帮助。

 error : function(XMLHttpRequest, textStatus, errorThrown) { alert(XMLHttpRequest.responseText+errorThrown+textStatus); $("#failUpload").removeClass("hide"); } 

祝一切顺利。

使用jsonp,您无法使用POST发送数据。 jQuery $ .ajax调用名称错误,因为它很混乱。 使用“JSON-P”数据进行$ .ajax调用时,函数会在DOM上注入脚本(

做这个:

  1. 仅使用带有JSON的$​​ .ajax,但要确保您在同一个域中,否则请参阅第2点。
  2. 如果您在localhost上并且您正在调用其他不同的域,那么您需要使用jsonp(但仅适用于GET请求)或在服务器上启用CORS。 看到这篇文章,因为我解释了类似你的问题: 本地AJAX调用远程站点在Safari中工作但在其他浏览器中不能

对我来说,这个问题的答案是删除:

 dataType: 'json' 

我在这里找到了答案: jQuery为ajax请求返回“parsererror”

我还将PHP fetch更改为:

 if (mysqli_query($con, $sql)) { echo "Record updated successfully"; } else { echo "Error updating record: " . mysqli_error($con); } 
 $.ajax({ url: "update.php", type: 'POST', dataType: 'jsonp', data: {key1: value1, key2: value2}, cache: false, crossDomain: false, error: function() { $("#failUpload").removeClass("hide"); }, success: function(data) { $("#succesUpload").removeClass("hide"); setTimeout(function() { $("#succesUpload").addClass("hide"); }, 5000); } }); 

把crossDomain:false,并试试这个。