JSON:收到的回应,但无法显示它在jQuery的 [英] JSON:Received response but unable to display it out in jQuery

查看:73
本文介绍了JSON:收到的回应,但无法显示它在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆