简体   繁体   English

AJAX 请求返回“实体不是格式正确的‘application/json’文档。 输入缺失或为空","code":"400"'

[英]AJAX Request returns 'The entity is not a well-formed 'application/json' document. Missing or empty input","code":"400"'

As I was trying to create an AJAX request in order to post some users against an external API, I encountered this issue preventing me from doing so.当我尝试创建 AJAX 请求以便针对外部 API 发布一些用户时,我遇到了阻止我这样做的问题。 I haven't used JS as much as PHP and I thought this would be a good time to start learning a bit more.我没有像 PHP 那样使用 JS,我认为这是开始学习更多的好时机。

The method I made checks first if the form contactForm has been fully completed, while it hasn't, it prevents the js from sending the data and showing the next form customerForm .我首先检查表单contactForm是否已完全完成的方法,如果没有,它会阻止 js 发送数据并显示下一个表单customerForm

If all the data has been properly filled, the js sends the data to the api via AJAX and hides that form to display the next one.如果所有数据都已正确填写,则 js 通过 AJAX 将数据发送到 api 并隐藏该表单以显示下一个。

(function() {
  'use strict';
  window.addEventListener('load', function() {
    var contactForm = document.getElementById('contactForm');
    var customerForm = document.getElementById('customerForm');

    var validation = Array.prototype.filter.call(contactForm, function(form) {
      contactForm.addEventListener('submit', function(event) {
        if (contactForm.checkValidity() === false) {
          event.preventDefault();
          event.stopPropagation();
        }
        contactForm.classList.add('was-validated');

        if (contactForm.checkValidity() === true) {
            customerForm.style.display = 'block';
            contactForm.style.display = 'none';
            event.preventDefault();
            (function() {
                var contactEmail = document.getElementById('contactEmail').value;
                var contactResellerId = 2;
                var contactName = document.getElementById('contactName').value;
                var contactLastName = document.getElementById('contactLastName').value;
                var contactCompany =  document.getElementById('contactCompany').value;
                var contactRegNum = document.getElementById('contactRegNum').value;

                $.ajax({
                    url: url,
                    type: 'POST',
                    crossDomain: true,
                    withCredentials: true,
                    data: JSON.stringify({
                        firstname: contactName,
                        lastname: contactLastName,
                        company: contactCompany,
                        email: contactEmail,
                        reseller_id: contactResellerId,
                        comregnum: contactRegNum
                    }),
                    dataType: 'json',
                    headers: {
                        'Authorization': 'Basic '+token,
                    }
                })
                .done(function (response) { alert('Se ha creado el contacto!'); })
                .fail(function (jqXHR, textStatus, errorThrown) { alert(jqXHR); });
            })();
        }
      }, false);
    });
  }, false);
})();

Whenever I try to execute this, it returns:每当我尝试执行此操作时,它都会返回:

The entity is not a well-formed 'application/json' document.该实体不是格式良好的“application/json”文档。 Missing or empty input","code":"400"输入缺失或为空","code":"400"

Form values are being correctly fetched into the variables, as console.log() proves.正如console.log()证明的那样,表单值被正确地提取到变量中。

What does the message mean by input?输入的消息是什么意思? Have I forgotten something?我是不是忘记了什么?

As always, thank you all for the help, have a nice weekend!一如既往,谢谢大家的帮助,周末愉快!

The asnwer to this one was actually quite easy: My AJAX request was indeed not well formed for it lacked some attributes.这个问题的答案其实很简单:我的 AJAX 请求确实格式不正确,因为它缺少一些属性。 This is how it should have been:本来应该是这样的:

$.ajax({
    url: url,
    method: 'POST',      <------- THIS
    crossDomain: true,
    withCredentials: true,
    data: JSON.stringify({
        firstname: contactName,
        lastname: contactLastName,
        company: contactCompany,
        email: contactEmail,
        reseller_id: contactResellerId,
        comregnum: contactRegNum
    }),
    dataType: 'json',
    contentType: 'application/json',   <------ THIS
    headers: {
        'Authorization': 'Basic ' + token,
    }
});

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

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