Skip to content Skip to sidebar Skip to footer

Unable To Send Json Object To Xmlhttprequest

I am unable to send JSON object to XMLHttpRequest(). However, if I send string data through send(), it works. For example, the following code works: var xhr = new XMLHttpRequest();

Solution 1:

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. 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. 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

Solution 2:

You are sending via a POST action, but then sending the data via a url string. If you want to send it that way you need to set it to GET.

Post a Comment for "Unable To Send Json Object To Xmlhttprequest"