2

目前,我们的软件由一个 winForms 应用程序组成,该应用程序调用许多 Web 服务,它们之间有数百个 Web 方法。

由于许多原因,我们正在转向纯 Web 架构(jQuery Mobile、jQuery、HTML5、LawnChair)。网页只会处理呈现,所有的业务逻辑都会发生在服务器端。所以我需要从网页调用这些网络服务。

我知道必须有很多方法可以做到这一点,例如我一直在试验 jQuery 为每个 Web 方法调用服务器端 HttpHandlers。由于 jQuery 使用 JSON,我不能直接调用 XML SOAP,因此需要在顶部添加一个新层,以便可以从 jQuery 调用。

然而,这是正确的做法吗?是否有另一种更合适的方法,即实现更少的努力,可能使用 WCF或其他我忽略的方法?

4

2 回答 2

2

您可以从 jQuery 使用 XML Web Services。它可以很好地处理 XML 消息。然而,在 JavaScript 中创建和读取 SOAP 消息很糟糕。

什么是合适的取决于你。但是,使用 WCF REST 甚至更好的ASP.NET MVC4 WebAPI将通过管道引导您的应用程序,并让您专注于相当快速地交付工作应用程序。但是,这将需要对您的原始服务进行一些重写(无论如何,您似乎注定要这样做)。

于 2012-05-01T12:35:29.837 回答
0

Not sure if this can help you but we use a webservice like ajax.asmx and we call it using jQueries ajax

Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel
Imports ClsLib
Imports Microsoft.WindowsAzure.StorageClient
Imports Microsoft.WindowsAzure

' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
<System.Web.Script.Services.ScriptService()> _
<System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
Public Class Ajax

<System.Web.Services.WebMethod()> _
Public Function function1(byval first as string, byval second as string) as string
    'do something here
    Return someJsonAsString
End Function

End Class

Something like that could help you I suppose.

But you can make your own classes to handle the existing SOAP XML services it jsut requries more client side code

var productServiceUrl = 'http://localhost:57299/ProductService.asmx?op=SaveProduct'; // Preferably write this out from server side

function beginSaveProduct(productID, productName, manufactureDate)
{
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>';

$.ajax({
url: productServiceUrl,
type: "POST",
dataType: "xml",
data: soapMessage,
complete: endSaveProduct,
contentType: "text/xml; charset=\"utf-8\""
});

return false;
}

function endSaveProduct(xmlHttpRequest, status)
{
 $(xmlHttpRequest.responseXML)
    .find('SaveProductResult')
    .each(function()
 {
   var name = $(this).find('Name').text();
 });
}

And parsing SOAP responses in jQuery.. I think there are plugins for that.

于 2012-05-01T11:39:49.297 回答