我有以下 jquery:
var xj=[{"name":"person","id":1},{"name":"jack", "id":2}];
$.post('/hex-jt/locations',xj , function(data){
console.log("this posted");
},'json');
看起来应该没问题。但它是这样传递给我的 Rails 应用程序的:
知道这是怎么回事吗?
最佳答案
您正在调用 jquery.post()
使用错误的参数数据,传递数组而不是字符串或 PlainObject
.
jQuery.post( url [, data ] [, success(data, textStatus, jqXHR) ] [, dataType ] )
data
Type: PlainObject or String
A plain object or string that is sent to the server with the request.
例如,您可以像这样修改它,将数组包装在一个对象中:
xj={"users":[{"name":"person","id":1},{"name":"jack", "id":2}]};
关于javascript - 不确定为什么这个 jquery 帖子不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18668781/