JSON:收到的回应,但无法显示它在jQuery的 [英] JSON:Received response but unable to display it out in jQuery
问题描述
我是新来的JSON和Web开发的东西。如果我无法present在适当方面的问题,所以原谅我。
I am new to JSON and web developing stuffs. So pardon me if I unable to present the problem in proper terms.
下面就是我收到了该网站JSON响应的情况,但无法显示一个错误响应数据提示了在控制台中。
Here's the situation where I received JSON response from the site, however unable to display the response data with an error prompted out in the console.
我试着在Firefox和Chrome。他们两个给了我不同的错误。
I tried in firefox and chrome. Both of them gave me different errors.
火狐=语法错误:缺少;语句之前
Firefox = "SyntaxError: missing ; before statement
Chrome浏览器=未捕获的SyntaxError:意外的标记:
Chrome = "Uncaught SyntaxError: Unexpected token :"
我已经打过电话,通过jQuery的API的两种类型。 下面是我的示例code。
I already tried two types of calling through jQuery API. Below are my sample code.
<html>
<body>
<button>Click Me!</button>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script>
$("button").click(function()
{
$.getJSON("http://rate-exchange.herokuapp.com/fetchRate?from=SGD&to=MYR&lang=en-us&format=json&jsoncallback=?", function(data) {
console.log(data);
var info = JSON.parse(data);
alert(rate);
});
})
</script>
</body></html>
<html>
<body>
<button>Click Me!</button>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script>
$("button").click(function loadRate()
{
$.ajax({
type:'GET',
url:"http://rate-exchange.herokuapp.com/fetchRate",
data:"from=SGD&to=MYR"+"&lang=en-us&format=json&jsoncallback=?",
dataType:'json',
success:function(resp){
var parse = JSON.parse(resp);
alert(parse.Rate);
}
});
})
</script>
</body></html>
和JSON的API,我指的是: http://rate-exchange.herokuapp.com/一>
And the JSON API i refer to is : http://rate-exchange.herokuapp.com/
响应数据是这样的:{要:马币,从:新元,价格:2.5666}
The response data is like this : {"To":"MYR","From":"SGD","Rate":"2.5666"}
推荐答案
1) VAR信息= JSON.parse(数据);
没有必要在这一行
1) var info = JSON.parse(data);
no need in this line
2)警报(率);
什么是率
3)。点击(函数loadRate()
的功能在这里不应该有任何的名称,只是。点击(函数()
3) .click(function loadRate()
function here should not have any name, just .click(function()
4) VAR解析= JSON.parse(RESP);
没必要,jQuery将自动当你告诉解析JSON的数据类型:JSON
4) var parse = JSON.parse(resp);
no need, jquery automatically parses json when you tell that dataType:'json'
这篇关于JSON:收到的回应,但无法显示它在jQuery的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!