尝试回到基础知识并通过 ajax 将 JSON 对象发送到我的 php.ini。我什至无法到达该部分,因为我收到 JSON 错误。这是 jquery 代码:
jQuery(".deletebutton").on("click", function() {
var employees = [
{ "firstName":"John" , "lastName":"Doe" },
{ "firstName":"Anna" , "lastName":"Smith" },
{ "firstName":"Peter" , "lastName": "Jones" }
];
var dataString = JSON.stringify(employees);
// Lets put our stringified json into a variable for posting
var postArray = {json:dataString};
jQuery.ajax({
type: 'POST',
url: 'index.php?option=com_recordings&task=deletevideos&format=raw',
data: postArray,
dataType: 'json',
success: function(data){
if (data == "blah")
alert(data);
}
});
});
我收到此错误(当我检查errorThrown
时):SyntaxError:JSON.parse:意外字符。我检查了 jsonlint.com 它是有效的 JSON。我做错了什么?
最佳答案
dataType
指的是请求 header ,而不是响应。如果您没有发回有效的 JSON,jQuery 不会喜欢它。您想要发送 JSON,但您可能想返回其他内容。只需删除 dataType
即可正常工作,除非服务器脚本出现错误。
关于jquery - 使用 jquery ajax 传递 json 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14265826/