简体   繁体   English

无法将JSON对象发送到XMLHttpRequest

[英]Unable to send JSON object to XMLHttpRequest

I am unable to send JSON object to XMLHttpRequest(). 我无法将JSON对象发送到XMLHttpRequest()。 However, if I send string data through send(), it works. 但是,如果我通过send()发送字符串数据,它将起作用。 For example, the following code works: 例如,以下代码有效:

var xhr = new XMLHttpRequest();
var url = 'https://xyz.info/api/contacts';
xhr.open("POST", url,true);
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

xhr.onreadystatechange = function() {//Call a function when the state changes.
     if(xhr.readyState == XMLHttpRequest.DONE && xhr.status == 200) {
                // Request finished. Do processing here.
     }
}
xhr.send("apikey=ee694eabf9e3&firstname=Raja1&lastname=Kumars&phone=123456");

However, if I try to send data using JSON, it posts nothing to the url. 但是,如果我尝试使用JSON发送数据,它将不会向URL发送任何内容。 The following code does not work. 以下代码不起作用。

var xhr = new XMLHttpRequest();
var url = 'https://xyz.info/api/contacts';
    xhr.open("POST", url,true);
    //xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xhr.setRequestHeader("Content-Type", "application/json");

    xhr.onreadystatechange = function() {//Call a function when the state changes.
    if(xhr.readyState == XMLHttpRequest.DONE && xhr.status == 200) {
            // Request finished. Do processing here.
        }
    }
    xhr.send(JSON.stringify({
                    'apikey' :'ee6915d4ee4b4df66bba82277e3',
                    'firstname' : 'Kumar',
                    'lastname' : 'Sunder',
                    'phone':'5557773334'
    }));        

You are sending via a POST action, but then sending the data via a url string. 您正在通过POST操作发送,但随后通过url字符串发送数据。 If you want to send it that way you need to set it to GET. 如果要以这种方式发送,则需要将其设置为GET。

You're sending very different information in your two calls. 您在两个呼叫中发送的信息完全不同。 Some sample code: 一些示例代码:

var _stringify = JSON.stringify({ 'apikey' :'ee6915d4ee4b4df66bba82277e3', 'firstname' : 'Kumar', 'lastname' : 'Sunder', 'phone':'5557773334' }); console.log(_stringify); var _orig = "apikey=ee694eabf9e3&firstname=Raja1&lastname=Kumars&phone=123456" var _encoded = encodeURI(_stringify); console.log(_orig); console.log(_encoded); when your original string is printed to the console log, it looks as you would expect: 当您将原始字符串打印到控制台日志时,它看起来与您期望的一样:

apikey=ee694eabf9e3&firstname=Raja1&lastname=Kumars&phone=123456 when the result of JSON.stringify is printed to the console, it returns: {"apikey":"ee6915d4ee4b4df66bba82277e3","firstname":"Kumar","lastname":"Sunder","phone":"5557773334"} That is, it comes complete with lots of extra double quote marks and left and right brackets. 当将JSON.stringify的结果打印到控制台上时, apikey=ee694eabf9e3&firstname=Raja1&lastname=Kumars&phone=123456 ,它返回: {"apikey":"ee6915d4ee4b4df66bba82277e3","firstname":"Kumar","lastname":"Sunder","phone":"5557773334"}也就是说,它附带了许多额外的双引号和左右括号。 If you want to send all of that as a string (as in your initial example), you would need to URI encode the result of the JSON.stringify call. 如果要以字符串的形式发送所有内容(如初始示例中所示),则需要对JSON.stringify调用的结果进行URI编码。 This is what happens with the "_encoded" variable, which contains: %7B%22apikey%22:%22ee6915d4ee4b4df66bba82277e3%22,%22firstname%22:%22Kumar%22,%22lastname%22:%22Sunder%22,%22phone%22:%225557773334%22%7D 这是使用“ _encoded”变量发生的情况,该变量包含: %7B%22apikey%22:%22ee6915d4ee4b4df66bba82277e3%22,%22firstname%22:%22Kumar%22,%22lastname%22:%22Sunder%22,%22phone%22:%225557773334%22%7D

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

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