0

我正在使用以下代码从 spring 3.0 rest webservice 读取 json 响应。

 $(document).ready(function() {
    jQuery.support.cors = true;
        $.ajax({
            url: 'http://localhost:8080/mobile-services/rest/images/',
            type: 'GET',
            dataType: 'jsonp',
            contentType:'application/json',
            success: function(data) { alert(""); },
            error: function() { alert('Error occured.. !'); },
            beforeSend: setHeader
        });
    });

    function setHeader(xhr) {
        xhr.setRequestHeader('Access-Control-Allow-Origin', '*');
        xhr.setRequestHeader('Content-Type', 'application/json');
    }

如果我使用 $.getJSON,我会得到有效的 JSON 作为响应。

 $(document).ready(function() 
 {
    alert("Within readky function..");
    $.getJSON('http://localhost:8080/mobile-services/rest/images/',
    function(json) 
    {
        alert(json); 
    });
  });

如果我从浏览器调用 web 服务,我会得到以下响应,这是一个有效的 JSON。

{"imageList":[{"itemName":"tv","picName":"image1.jpg","price":20,"id":0},{"itemName":"radio","picName":"image2.jpg","price":5,"id":0}]}

我还想提一件事。$.getJSON 在 chrome 和 firefox 上运行良好,但在 IE 上失败。

请建议。

4

0 回答 0