我有一个 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(内部服务器错误)
当我打开它时,我发现了这个:
_.ajaxTransport.Y.cors.a.crossDomain.send @ jquery.js:26
我已经搜索并找到了有关跨域调用的内容,您必须使用 jsonp 等,但没有成功。谢谢!
最佳答案
错误使用以下函数。它将显示确切的问题。我认为这会有所帮助。
error : function(XMLHttpRequest, textStatus, errorThrown) {
alert(XMLHttpRequest.responseText+errorThrown+textStatus);
$("#failUpload").removeClass("hide");
}
祝一切顺利
关于php - jQuery Ajax 到 PHP MySQL - 跨域内部服务器错误 (500),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30545389/