我遇到了問題,我無法使用$ .ajax()調用來訪問我的php文件。我總是得到 0這是一個jqXHR.status是我的代碼:無法使用ajax訪問php
函數$。阿賈克斯()調用:
function updateClubInfo(text) {
var data = text;
$.ajax({
type: 'POST',
dataType: 'json',
url: '../php/updateInfo.php',
data: {
infoText: data,
action: "update"
},
success: function() {
// do nothing
alert('success');
},
error: function(jqXHR, exception) {
if (jqXHR.status === 0) {
alert('Not connect.\n Verify Network.');
} else if (jqXHR.status == 404) {
alert('Requested page not found. [404]');
} else if (jqXHR.status == 500) {
alert('Internal Server Error [500].');
} else if (exception === 'parsererror') {
alert('Requested JSON parse failed.');
} else if (exception === 'timeout') {
alert('Time out error.');
} else if (exception === 'abort') {
alert('Ajax request aborted.');
} else {
alert('Uncaught Error.\n' + jqXHR.responseText);
}
}
});
}
我已經建立類似人在此請求我的項目,但這是唯一一個不起作用的 。在這裏,PHP文件,我想達到:
PHP代碼片段(updateInfo.php):
<?php
$action = $_POST['action'];
$text = $_POST['data'];
myLog($action);
myLog($text);
echo "hello";
/*
* Writes entry in Logfile
*/
function myLog($data) {
$text = @file_get_contents('log.txt');
$text = $text . "\n" . $data;
@file_put_contents('log.txt', $text);
}
?>
當我嘗試在URI回聲輸出達到這個PHP文件。所以我認爲 問題應該在ajax調用中。
有人有一個想法我做錯了什麼?我很感激任何幫助。
THX很多的幫助 克里斯
您使用Firefox或Chrome調試工具會得到什麼回覆?你會在響應中收到任何錯誤消息嗎?什麼http狀態碼? – Gohn67
我不確定您是否可以通過「../」訪問您的腳本 - 我自己從未使用過腳本。嘗試改爲使用「/path/to/php/updateInfo.php」路徑 – Stefan
在Chrome中,您可以使用開發工具中的網絡選項卡。嘗試運行你的表單並尋找你的ajax調用並檢查結果。 – Gohn67