I am in newbie in JavaScript, JQuery and Ajax coding. I am using JQuery $.ajax method to invoke asyn REST call. Somehow I am not able to receive HTTP response JSON data.
I can see the below alert result. alert(data) method result is [Object Object] alert(data.toSource()) method result is ({"key1","value1"}) alert($.parseJSON(data)) method result is nothing
I have tested the below code in both firefox and chrome browsers.
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"</script>
</head>
<body>
<form id="foo">
<label for="bar">A bar</label>
<input id="bar" name="bar" type="text" value="" />
<input type="submit" value="Send" />
</form>
<!-- the result of the search will be rendered inside this div -->
<div id="result"></div>
<script>
$("#foo").submit(function(event) {
event.preventDefault();
$("#result").html('');
var values = $(this).serialize();
$.ajax({
url: "resources/helloWorld",
type: "GET",
dataType: 'json',
success: function(data){
alert(data);
alert(data.toSource());
var r = $.parseJSON(data);
alert(r);
$("#result").html(data);
},
error:function(){
$("#result").html('there is error while submit');
}
});
});
</script>
</body>