2014-10-01 27 views
0

我有一些ADO.NET數據服務運行一段時間,現在想通過jQuery從Web客戶端使用它們。當我嘗試做以下,錯誤處理程序總是被稱爲:使用jQuery消費.NET DataService

$.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); } 
    } 
); 

看着小提琴手,數據正確以XML格式返回,但在錯誤處理程序總是被調用。 jQuery能否解析application/atom + xml供稿響應?

回答

0

一個JavaScript庫,這裏是一個AJAX調用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/

上面的源代碼給你一步如何分步說明,如何來源不起作用,谷歌「如何從ajax做肥皂調用」,會有多個可用的鏈接到這個確切的查詢

+0

也使用'SoapUI'來獲得正確的XML正文發送到服務器,'SoapUI'是一個測試webservices和其他肥皂相關的東西的偉大工具 – Pierre 2014-10-10 15:40:12