[英]jQuery Ajax to PHP MySQL - Cross Domain Internal Server 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
我已經搜索並找到了有關跨域調用的內容,您必須使用jsonp等,但無法解決。 謝謝!
使用以下功能進行錯誤處理。 它將顯示確切的問題。 我認為這會有所幫助。
error : function(XMLHttpRequest, textStatus, errorThrown) {
alert(XMLHttpRequest.responseText+errorThrown+textStatus);
$("#failUpload").removeClass("hide");
}
祝一切順利。
使用jsonp時,您無法使用POST發送數據。 jQuery $ .ajax調用名稱錯誤,因為它很混亂。 當您使用功能為“ JSON-P”的數據進行$ .ajax調用時,會在您的DOM上插入一個腳本(<script src =“ example-domain.com/do-this-task.php?callback=my_callback_on_js>)。
做這個:
對我來說,這一答案是刪除:
dataType: 'json'
我在這里找到了答案: jQuery為ajax請求返回“ parsererror”
我也將PHP提取更改為:
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,然后嘗試使用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.