使用 MooTools *在请求正文中* POST JSON
我正在尝试在我的应用程序中的 URL 之间发布 JSON。接收 URL 期望请求正文中包含 JSON,并使用请求正文中的 JSON 进行响应。问题是我似乎无法使用 Mootools Request.JSON 在正文中发送 JSON。这就是我所得到的:
// formObj is an object constructed from a form
var request = new Request.JSON({
url: "/api/object.new",
urlEncoded: false,
onRequest: function(){
// swap submit button with spinner
},
onComplete: function(jsonObj) {
// work with returned JSON
},
body: JSON.encode(formObj)
});
request.setHeader("Content-Type", "application/json");
request.post();
服务器返回 500 错误:
BadValueError: Property name is required
这意味着 request.name
返回 None
这意味着服务器没有获取我的 JSON。
使用 HTTPClient 将 JSON.encode(formObj) 的输出粘贴到 body 字段中会产生所需的结果。
I'm trying to post JSON between URLs in my app. The receiving URL expects JSON in the body of the request and responds with JSON in the body of the request. The problem is I can't seem to send JSON in the body using Mootools Request.JSON. This is what I have:
// formObj is an object constructed from a form
var request = new Request.JSON({
url: "/api/object.new",
urlEncoded: false,
onRequest: function(){
// swap submit button with spinner
},
onComplete: function(jsonObj) {
// work with returned JSON
},
body: JSON.encode(formObj)
});
request.setHeader("Content-Type", "application/json");
request.post();
The server returns a 500 error:
BadValueError: Property name is required
Which means that request.name
is returning None
which means that the server is not getting my JSON.
Using HTTPClient to paste the output of JSON.encode(formObj) into the body field produces the desired results.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
body 不是 Request 的有效 mootools 属性。使用
data: blah
来代替。就目前而言,数据是空的,所以难怪你在服务器端什么也得不到......body is not a valid mootools property for Request. use
data: blah
instead. as it stands, data is empty so no wonder you get nothing on the server side...