如何处理来自Ajax调用的JSON响应 [英] How to process json response from ajax call

查看:132
本文介绍了如何处理来自Ajax调用的JSON响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的web服务返回的JSON对象类似以下

  [ABC,某某,option_3]
 

即。当我把这个地址在Chrome浏览器的http://本地主机:8088 / REST / getOptions 我收到上述

我想读这个浏览器,这样我可以创建一个下拉选项...但我什么也得不到从下面code入手:

 <!DOCTYPE HTML>
< HTML LANG =EN>
< HEAD>
    <元字符集=utf-8>
    &所述;脚本的src =的http://$c$c.jquery.com/jquery-1.9.1.js>&所述; /脚本>
    <脚本类型=文/ JavaScript的>
        URL =HTTP://本地主机:8088 / REST / getOptions;
        VAR ajaxResult = NULL;
        功能的getData(yt_url){
            $就
            ({
                键入:GET,
                网址:yt_url,
                数据类型:JSON,
                异步:假的,
                成功:函数(响应){
                    ajaxResult =响应;
                }
            });
        }
        的getData(URL);

    警报(ajaxResult);

    < / SCRIPT>
< /头>
<身体GT;
< /身体GT;
< / HTML>
 

我总是的警告框。我仔细检查与fiddler2的Web服务请求/响应正在经历很好,我甚至可以拦截JSON对象互联网服务和浏览器之间。

我也试过

  VAR OBJ = JSON.parse(ajaxResult);
警报(OBJ);
 

我空再次获取。

我已经看了 Ajax调用的JSON响应和类似的问题。请看看我的JSON是有点不同的,因此无论是这些解决方案将不会在我的情况申请还是我没跟着他们。

我是新来的AJAX和Java脚本,请告知。

编辑: addding JERSEY webservice的code供应此休息/ getOptions终点

  @GET
@Path(getOptions)
@Produces(MediaType.APPLICATION_JSON)
公开名单<字符串> getOptionsJson(){
    //建立alloptions名单,其中,字符串>
    返回alloptions;
}
 

解决方案

更新

您编辑的问题;你不能使用数据类型:JSON,因为无论是通过使用代理在同一个域或通过的 CORS

在使用数据类型:JSONP则不能使用异步:假;这是因为它并没有真正使用 XMLHtt prequest ,但使用<脚本> 标记,使它做跨域。我相信jQuery的简单的忽略异步设置,而不发出警告。

因此​​,警报()将永远是空的,你应该将它成功的回调中。

要支持JSONP Web服务必须包装返回的数据转换成一个函数调用,通过回调 GET参数,例如:

 回调([1,2,3])
 

如果您的Web服务不支持这一点,如前面提到的,您需要使用 CORS < /一>,例如:添加此响应头:

 访问控制 - 允许 - 产地:*
 

My webservice returns a JSON object like following

["abc","xyz","option_3"]

i.e. when I put this address in chrome browser http://localhost:8088/rest/getOptions I get above.

I am trying to read this in browser so that I can create a drop-down option... but I get nothing from below code to start with:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
    <script type="text/javascript">
        url="http://localhost:8088/rest/getOptions";
        var ajaxResult = null;
        function getData(yt_url){
            $.ajax
            ({
                type: "GET",
                url: yt_url,
                dataType:"json",
                async: false,
                success: function(response){
                    ajaxResult = response;
                }
            });
        }
        getData(url);

    alert(ajaxResult);

    </script>
</head>
<body>
</body>
</html>

I always get null in alert box. I double checked with fiddler2 that webservice request/response is going through fine, I even can intercept json object between webservice and the browser.

I also tried

var obj = JSON.parse(ajaxResult);
alert(obj);

I get null again.

I have already looked at Ajax call for the json response and similar questions. Please see my JSON is a bit different, so either those solution won't apply in my case or I didn't follow them.

I am a new to AJAX and Java script, please advise.

EDIT: addding JERSEY webservice code which serves this rest/getOptions end point

@GET
@Path("getOptions")
@Produces(MediaType.APPLICATION_JSON)
public List<String> getOptionsJson() {
    //build alloptions List<String>
    return alloptions ;
}

解决方案

Update

You edited the question; you can't use dataType: 'json' because of cross-domain issues that can only be overcome by either using a proxy on the same domain or via CORS.

When you use dataType: 'jsonp' you can't use async: false; this is because it doesn't really use XMLHttpRequest but uses the <script> tag to make it do cross-domain. I believe jQuery simply ignores the async setting without issuing a warning.

Therefore, the alert() will always be empty and you should move it inside the success callback.

To support JSONP your web service must wrap the returned data into a function call, identified by the callback GET parameter, e.g.

callback([1, 2, 3])

If your web service doesn't support this, as mentioned before, you would need to use CORS, e.g. add this response header:

Access-Control-Allow-Origin: *

这篇关于如何处理来自Ajax调用的JSON响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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