我在JavaScript,JQuery和Ajax編碼的新手。 我正在使用JQuery $ .ajax方法來調用異步REST調用。 不知何故,我無法接收HTTP響應JSON數據。
我可以看到下面的警報結果。 alert(data)方法結果爲[Object Object] alert(data.toSource())方法結果爲({「key1」,「value1」}) alert($。parseJSON(data))方法結果爲空
我已經在firefox和chrome瀏覽器中測試了下面的代碼。
<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>
要退有效的JSON? – ajtrichards
我的json響應是{「key1」:「value1」} –
你可以發佈甚至是json文件。這將很容易分析。 – iraycd