使用jQuery ajax的跨域OData请求

时间:2013-06-19 06:52:15

标签: asp.net ajax cross-domain jsonp odata

我正在尝试使用jQuery ajax进行跨域OData请求,如下所示。

Jquery的

$(function () {
$.ajax({
    url: 'http://localhost:62526/OdataServer/Odata.svc/vw_listing&$format=json&$callback=?',
    dataType: "jsonp",
    jsonpCallback: "addData"
 });
});

function addData(jsonString) {
for (var i = 0; jsonString.d[i] != null; i++) {
    $("#itemParent").append("<li>" + jsonString.d[i].Address + "</li>");
 }
}

数据服务

public class Odata : DataService< testEntities >
{

 public static void InitializeService(DataServiceConfiguration config)
 {
    config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
    config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
 }
}

当我尝试使用AJAX调用dataservice时,我收到400错误。

 Failed to load resource: the server responded with a status of 400 (Bad Request)
 http://localhost:56403/OdataServer/Odata.svc/vw_listing&$format=json&$callback=addData?_=1371713035531

2 个答案:

答案 0 :(得分:1)

这解决了我的问题

$(function () {
$.getJSON('url?' +'$format=json&$callback=?',
    function (response) {
        $.each(response.d, function (index, value) {
          ............................

        })
    });
});

Shanz

答案 1 :(得分:0)

您的网址的参数查询字符串需要以?

开头
'http://localhost:62526/OdataServer/Odata.svc/vw_listing?$format=json&$callback=?'