【发布时间】:2012-06-10 10:28:56
【问题描述】:
我正在尝试调用一个接受 JSON 数据的 php 脚本,将其写入文件并使用 jQuery/AJAX 调用返回简单的文本响应。
jQuery 代码:
$("input.callphp").click(function() {
var url_file = myurl;
$.ajax({type : "POST",
url : url_file,
data : {puzzle: 'Reset!'},
success : function(data){
alert("Success");
alert(data);
},
error : function (jqXHR, textStatus, errorThrown) {
alert("Error: " + textStatus + "<" + errorThrown + ">");
},
dataType : 'text'
});
});
PHP 代码:
<?php
$thefile = "new.json"; /* Our filename as defined earlier */
$towrite = $_POST["puzzle"]; /* What we'll write to the file */
$openedfile = fopen($thefile, "w");
fwrite($openedfile, $towrite);
fclose($openedfile);
echo "<br> <br>".$towrite;
?>
但是,调用永远不会成功,并且总是会给出错误提示“错误:[Object object]”。
注意
此代码运行良好。我正在尝试执行跨域查询 - 我将文件上传到同一台服务器并且它有效。
【问题讨论】: