Ajax 开机自检调用导致 422 错误



我正在尝试在站点(我已注册的地方)的表单上发布数据,以下是我的代码:

$.ajax({
         url:'https://api.contentstack.io/v2/content_types/the_lazy_goose/entries/',
         type: "POST",
         contentType: "application/json",
         data: JSON.stringify({
                title: "John", 
                url: "2pm", 
                multi_line: "Random Text using Post call"
            }),
        headers:{
                    access_token: 'xxxxxxxxxxx',
                    api_key: 'xxxxxxxxx'
                },
        success: function() {
           alert("success");
        },
        error: function() {
               alert("ERROR");
            },
    });

这会导致 422(无法处理的实体),不确定哪里出了问题。*我已经 http://www.restpatterns.org/HTTP_Status_Codes/422_-_Unprocessable_Entity 并了解"request_entity"和"请求语法"很好

该网站似乎 https://contentstackdocs.built.io/rest/api/content-management-api/支持 Json 数据

422 在参数验证失败时使用。您可能缺少一些必需的参数。

另外,我认为数据JSON.stringify是不必要的。

所以我浏览了他们的contentstack.js文件,并使用他们的Request方法(而不是ajax)来发布数据。架构也是错误的。所以这是我最后使用的代码:

function Request(options, callback) {
        HTTPRequest = XMLHttpRequest;
        var xhr = new HTTPRequest(),
            method = options.method || "POST",
            url = options.url,
            headers = options.headers;
        // make a request
        xhr.open(method, url, true);
        // set headers
        xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
        for (var header in headers) {
            xhr.setRequestHeader(header, headers[header]);
        }
        // send stringify data
        if (options.body && method == "POST" || method == "PUT") {
            if (typeof options.body === 'object') {
                console.log("sending in data: options.body");
                xhr.send(JSON.stringify(options.body));
            } else {
                xhr.send(options.body);
            }
        } else {
            xhr.send();
        }
        // collect response
        xhr.onreadystatechange = function() {
            if (xhr.readyState === 4) {
                var data = xhr.responseText;
                try {
                    data = JSON.parse(data);
                } catch (e) {
                    console.error('Could not parse the response received from the server.');
                }
                if (xhr.status >= 200 && xhr.status < 300) {
                    callback(null, data);
                } else {
                    callback(data, null);
                }
            }
        };
    }
    var options =  {
            url: 'https://api.contentstack.io/v2/content_types/the_lazy_goose/entries/',
            method:'POST',
            headers:{
                        access_token: 'xxxxxxx', //put auth token here
                        api_key: 'xxxxxxx'
                    },
            body: {
                      "entry": {
                        "title": "examplexx1",
                        "url": "/example10xxx1",
                        "multi_line": "multiline...!",
                        "_comment": "example comment"
                      }
                }
        };

相关内容

最新更新