您好我正在尝试在我现有的 JSON 文件中编辑和保存一些数据,这是代码 -
var newData = { mailTo: 'abc@123.com'};
$.ajax({
type: 'POST',
url: 'jsonHandler.php',
data: newData,
}).done(function( formData ) {
formData = eval('(' + formData + ')');
$('#mail').val( formData.load[0].mailTo );
});
这适用于 chrome 和 IE,但是当我使用 firefox 运行它时,JSON 没有得到更新。
这是在 Firefox 中生成的请求标头(使用 firebug)
Request Headers
Accept */*
Accept-Encoding gzip, deflate
Accept-Language en-US,en;q=0.5
Content-Length 12
Content-Type application/x-www-form-urlencoded; charset=UTF-8
Host localhost
Referer http://localhost/gandu/
User-Agent Mozilla/5.0 (Windows NT 6.1; rv:21.0) Gecko/20100101 Firefox/21.0
X-Requested-With XMLHttpRequest
jsonHandler.php 代码
<?php
require_once('JSON.php');
$json = new Services_JSON();
$json_data = json_decode(file_get_contents('formData.json'), true);
if (isset( $_POST['formId'])) {
$json_data['load'][0]['formId'] = $_POST['formId'];
} else {
echo 'No text found';
}
if (isset( $_POST['mailTo'])) {
$json_data['load'][0]['mailTo'] = $_POST['mailTo'];
} else {
echo 'No text found';
}
$output = ($json->encode($json_data));
file_put_contents('formData.json', $output);
print ($output);
?>
有什么我做错了吗。
萤火虫中的错误 -
SyntaxError: missing ) in parenthetical
a()jquery.min.js (line 2)
e = Object { url="jsonHandler.php", isLocal=false, global=true, more...}
n = Object { readyState=4,
responseText="No text found{"load":[{..."qq","fmessage":"qq"}]}", status=200, more...}
r = Object { text="No text found{"load":[{..."qq","fmessage":"qq"}]}"}
a()jquery.min.js (line 2)
e = 200
n = "OK"
s = Object { text="No text found{"load":[{..."qq","fmessage":"qq"}]}"}
a = "Date: Sat, 01 Jun 2013 ...ntent-Type: text/html\r\n"
a()jquery.min.js (line 2)
e = readystatechange
i = undefined
[Break On This Error]
(No text found{"load":[{"formId":"form2","mailTo":"def@123.com"}]
显示一个警告框说错误。