I have some troubles whit jquery ajax call, infact I tried to perform a post call passing like data a string variable:
myVar = 'Hello';
$.ajax(
type: 'POST',
url : 'https://...',
data: myVar,
success : function(data) {
},
complite: function() {...},
error: function(err) {...}
)
If I inspect the http call I can see that the payload is:
'Hello': ""
I don’t know how it is possible and how fix the problem.
2
Answers
i think you are passing payload in the wrong formate.
On the server side you will get the value in the key ‘name’ you can fetch the value using ‘name’ key.
jQuery, by default, will put a
Content-Type: application/x-www-form-urlencoded
header on data it sends via Ajax.You, however, are passing a plain text string.
You need to either change the
Content-Type
to match the data you are sending or the data you are sending to match theContent-Type
. (Or you could change both).The important things are:
So you might:
or, since jQuery will encode objects as URL encoded data:
or, if you want multipart data:
or, for JSON