![](/img/trans.png)
[英]Ajax form not working because of json (parsererror thrown: SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data)
[英]Ajax error with the same data (parsererror: SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data)
我有兩個頁面使用相同的js文件來調用某些PHP文件,並以JSON格式從那里獲取數據。 盡管從PHP文件中獲取的數據與從中獲取的數據完全相同,但第二頁上的Ajax在JSON數據的第1行第1列返回'parsererror'SyntaxError:JSON.parse:意外字符。
$.ajax({
type: 'POST',
dataType: "json",
data: {objtyp: this.objtyp, objid: this.objid},
url: '/admin/getfieldsadd.php',
success: function(data) {
//not going to happen
},
error: function (xhr, status, text) {
switch (status) {
case 404:
alert('File not found');
break;
case 500:
alert('Server error');
break;
case 0:
alert('Request aborted');
break;
default:
alert('Unknown error: ' + status + " " + text);
}
}
那么有人遇到過同樣的問題嗎?
這聽起來讓人想起了可怕的BOM 。 該鏈接的節選:
在使用Unicode字符編碼的頁面的開頭,您可能會找到一些表示Unicode代碼點U + FEFF BYTE ORDER MARK(縮寫為BOM)的字節。
BOM正確使用后將不可見。
也許檢查文件的編碼是否設置為UTF8 Without BOM 。
也許是mime類型錯誤。
嘗試在AJAX調用中添加beforeSend屬性,如下所示:
$.ajax({
type: 'POST',
dataType: "json",
data: {objtyp: this.objtyp, objid: this.objid},
url: '/admin/getfieldsadd.php',
beforeSend: function(x) {
if(x && x.overrideMimeType) {
x.overrideMimeType("application/json");
}
},
...
}
看來問題出在jQuery版本中。 現在,它已更新,一切似乎工作正常。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.