I am trying to send a variable that contains a name to a server using the POST method. This is suppose to be Asynchronous. I have the following code written but I am getting an error, SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data and honestly it isn't too helpful.
I downloaded firebug and firebug gets me a little closer as it says the error is on line 84 column 17 (I wasn't sure what to make of column so I counted 17 characters in). If this is the case, then it says my problem is starting with the word parse.
I am new to AJAX so I am not sure what you guys do and don't need to see, but I will start by showing this bit of code where I think the problem resides.
function sendByPost()
{
var name = getName();
var req = new XMLHttpRequest();
req.open("POST", "http://httpbin.org/post", true);
req.setRequestHeader('Content-Type', 'application/json');
req.send('{"Tom"}');
console.log(JSON.parse(req.responseText));
}
function getName()
{
return document.getElementById("myName").value;
}
I also do not want it to say Tom on the 7th line down and my intention is for it to say whatever the user types in. So I setup a name variable that would get the value inside the text box.
Doing the following doesn't work though:
req.send('{name}');