PHP AJAX POST请求返回错误
问题描述:
我正在使用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);
}
答
研究之后,我的问题是,我不能上传与AJAX的文件。我不得不使用隐藏的iframe来解决问题。
StackOverflow的语法高亮显示你显然在某处存在双引号问题... – MonkeyZeus
是的,我在缩进代码时犯了一个错误。现在应该修好了。 – ahalbert
它仍然是错误的 – MonkeyZeus