I'm pretty sure this has a simple solution that I'm missing. I have the following ajax script.
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
function postForm() {
var ret = $('#test').val();
$.ajax({
type: 'POST',
url: 'http://10.0.0.8:9000/demo',
data: '{"name" : '+ret'}',
contentType: "application/json; charset=utf-8",
})
}
</script>
</head>
<body>
<form id="ajaxForm" onSubmit="postForm(); return false;" method="post">
<input id="test" type="text" name="name" value="Hello JSON" />
<input type="submit" value="Submit JSON" />
</form>
</body>
</html>
I've pulled the value of the input with id='test' put it in the ret variable. Then I'm trying to insert it into the data attribute and send it via ajax. I've tried various assortments of quotes and nothing seems to be working.... what am I doing wrong?
Thanks ahead
dataparameter as a string. Justdata : { name : ret }.application/json). The thing that's wrong is that he built this JSON manually using string concatenations instead of using theJSON.stringifymethod as shown in my answer below.