0
我正在使用PHP來處理來自窗體的AJAX POST請求,並且需要它發回一個JSON對象。我的PHP代碼。PHP AJAX POST請求返回錯誤
<?php
function main(){
$a = $_POST['args'];
$myFile = "jsonargs.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $a);
fwrite($fh, "\n");
fwrite($fh, "-o ./json\n");
fwrite($fh, "-j\n");
fclose($fh);
$output = shell_exec('command 2>&1; ./syn-bin/synoptic-jar.sh -c jsonargs.txt ' . $_FILES["file"]["tmp_name"]);
$json = file_get_contents('./json.json');
header('Content-Type: application/json');
echo json_encode($json);
}
main();
?>
但是,當我使用jquery的ajax表單插件進行ajax調用時,我的錯誤函數被調用。如果我提交它作爲一個正常的形式,而不是做一個Ajax調用,它會正常工作。我如何正確返回一個JSON對象?
我的AJAX請求:
// prepare the form when the DOM is ready
$(document).ready(function() {
var options = {
target: '#output1', // target element(s) to be updated with server response
beforeSubmit: showRequest, // pre-submit callback
success: showResponse, // post-submit callback
error: showResponse
};
// bind form using 'ajaxForm'
$('#myForm').ajaxForm(options);
});
// pre-submit callback
function showRequest(formData, jqForm, options) {
alert('About to submit: \n\n' + queryString);
return true;
}
// post-submit callback
function showResponse(responseText, statusText, xhr, $form) {
alert('status: ' + statusText + '\n\nresponseText: \n' + responseText +
'\n\nThe output div should have already been updated with the responseText.');
console.log(responseText);
}
StackOverflow的語法高亮顯示你顯然在某處存在雙引號問題... – MonkeyZeus
是的,我在縮進代碼時犯了一個錯誤。現在應該修好了。 – ahalbert
它仍然是錯誤的 – MonkeyZeus