jquery - javascript - ajax request null data -
i trying make ajax request server. request send successfully, parameters not appropriate in request body.
parameters shown in body browser's network tool:
{"name":"","message":"","time":1464807897670}
the name <input>
, message <textarea>
fields empty, although should not be.
here's html input fields:
<div id="writepanel"> <p>your name</p> <input id="writename" type="text"> <p>message</p> <textarea id="writearea"></textarea> <div id="sendbutton">send</div> </div>
here's js code:
$(document).ready(function() { var data = { name: document.getelementbyid("writename").value, message: document.getelementbyid("writearea").value, time: new date().gettime() }; $("#sendbutton").click(function () { $.ajax({ type: "post", url: "message.receive", contenttype: "application/json", data: json.stringify(data), success: function (response) { // ... } }) }) })
what missing?
it looks setting values @ beginning rather when button pressed. try setting values in data object in click function before ajax call:
$("#sendbutton").click(function () { var data = { name: document.getelementbyid("writename").value, message: document.getelementbyid("writearea").value, time: new date().gettime() }; $.ajax({ type: "post", url: "message.receive", contenttype: "application/json", data: json.stringify(data), success: function (response) { // ... } }) })