使用jQuery使用.NET DataService

时间:2014-10-01 15:11:42

标签: jquery wcf-data-services

我已经运行了一段时间的ADO.NET数据服务,现在想通过jQuery从Web客户端使用它们。当我尝试执行以下操作时,始终会调用错误处理程序:

$.ajax(
    {
        type: "GET",
        url: "Service.svc/Customers()",
        contentType: "application/atom+xml;type=feed;charset=utf-8",  
        dataType: "xml",
        xhrFields: { withCredentials: true },
        error: function (jqXHR, textStatus, errorThrown) { alert(jqXHR.response + textStatus + errorThrown); },
        success: function (xml) { alert(xml); }
    }
);

观察fiddler,数据以XML格式正确返回,但始终调用错误处理程序。 jQuery能不解析application / atom + xml feed响应吗?

2 个答案:

答案 0 :(得分:0)

您可以尝试使用datajs,这是一个用于不同OData版本的javascript库http://datajs.codeplex.com/

答案 1 :(得分:0)

这是一个Ajax Call for JavaScript

$.ajax({
    url: "Login.aspx/Logout",
    type: "POST",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (o) {
        window.location.href = "Login.aspx";
    },
    error: function (o) {
        logoutSession();
    }
});

任何aspx页面上的服务器端方法。

[WebMethod]
public static string Logout()
{
    HttpContext.Current.Session["User"] = null;
    return "Success";
}

调用wsdl服务时

$.ajax({
    url: "Service.svc/Customers", 
    type: "POST",
    dataType: "xml", 
    data: soapMessage, 
    processData: false,
    contentType: "text/xml; charset=\"utf-8\"",
    success: function (xml) { alert(xml); }, 
    error: function (jqXHR, textStatus, errorThrown) { alert(jqXHR.response + textStatus + errorThrown); }
});

soapMessage变量将包含如下所示的代码:

var soapMessage =
'<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">\
<soap:Body> \
<SaveProduct xmlns="http://sh.inobido.com/"> \
<productID>' + productID + '</productID> \
<productName>' + productName + '</productName> \
<manufactureDate>' + manufactureDate + '</manufactureDate> \
</SaveProduct> \
</soap:Body> \
</soap:Envelope>';

来源http://openlandscape.net/2009/09/25/call-soap-xm-web-services-with-jquery-ajax/

以上来源为您提供分步说明如何,如果来源不起作用,Google&#34;如何从ajax&#34;进行肥皂调用,将会有多个可用链接指向此确切的查询