简体   繁体   English

使用MooTools在请求正文中POST JSON

[英]POST JSON *in Request Body* using MooTools

I'm trying to post JSON between URLs in my app. 我正在尝试在应用程序的URL之间发布JSON。 The receiving URL expects JSON in the body of the request and responds with JSON in the body of the request. 接收URL在请求的主体中期望JSON,并在请求的主体中响应JSON。 The problem is I can't seem to send JSON in the body using Mootools Request.JSON. 问题是我似乎无法使用Mootools Request.JSON在体内发送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: 服务器返回500错误:

BadValueError: Property name is required

Which means that request.name is returning None which means that the server is not getting my JSON. 这意味着request.name返回None ,这意味着服务器未获取我的JSON。

Using HTTPClient to paste the output of JSON.encode(formObj) into the body field produces the desired results. 使用HTTPClient将JSON.encode(formObj)的输出粘贴到主体字段中会产生所需的结果。

body is not a valid mootools property for Request. 主体不是Request的有效mootools属性。 use data: blah instead. 使用data: blah as it stands, data is empty so no wonder you get nothing on the server side... 就目前而言,数据是空的,所以难怪您在服务器端什么也没得到...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM