jQuery ajax - SyntaxError: JSON.parse with fine json
jQuery ajax - SyntaxError: JSON.parse with fine json
我有问题。我从php返回到ajax调用时遇到一个错误。
SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
在php中我做这个
echo json_encode(array("message1" => "Hi","message2" => "Something else"));
我的ajax调用
$.ajax({
type: "POST",
url: "s_save.php",
data: data,
cache: false,
dataType: "json",
beforeSend: function() {
console.log("beforesend: "+data);
},
success: function(){
console.log("form search in db: "+data);
sendMail(mailName,mailAdress, customertype );
},
error: function (xhr, ajaxOptions, thrownError) {
console.log(xhr.status);
console.log(thrownError);
console.log(ajaxOptions)
}
});
}
现在,如果我看看我的萤火虫,我会看到答案,什么对我来说很好。
{"message1":"Hi","message2":"Something else"}
错误部分,但要这样说。
200
SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
parsererror
我不明白。json返回对我来说很好。为什么我会得到错误?
编辑:
在得到第一个答案后,我对php文件进行了一些更改,但没有帮助header('Content-Type:application/json');
$output = array("data" => "Hi","message2" => "Something else");
$output = str_replace(' ', '', $output);
echo json_encode($output);
编辑2
这就是解决问题的办法。感谢大家的帮助!
Save the file as UTF-8 without BOM. – Álvaro G. Vicario 12 mins ago
设置dataType:"html"而不是json。
相关文章:
- Fighting with FRP
- issue with FB.Event.subscribe
- JSON.parse没有'不能使用Javascript
- geolocation-marker.js conflict with markerclusterer.js
- Angular 2.0 with JavaScript or TypeScript?
- 为什么不推荐使用“with”?是否有更好或其他方法可以“下降”到对象的命名空间
- WebComponentsJS with IE10
- Meteor with Parse - 在 Android 设备上运行 - 未定义 Parse
- Json parse with in JavaScript
- Parse query innerQuery with relation
- parse HTMLElmentObject with innerHTML in javascript
- JSON.stringify / parse oddness with quote marks
- jQuery ajax - SyntaxError: JSON.parse with fine json
- Working with Parse.com dates
- Parse vcard with Javascript
- Titanium Appcelerator parse Youtube JSON with API
- Angular js with Parse
- Parse json with javascript
- parse google place json with javascript
- Parse getJson with variable