我正在测试我的第一个 WCF 服务。它只有一个方法,该方法等于作为参数给出的两个值并返回结果。
当我使用Visual Studio提供的Test Client时,它工作正常,但是当我使用jQuery开发自己的JS客户端时,我不知道如何读取数据或如何调用此方法来获取并显示在一个警报。
代码如下:
ImiServicio.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace primerwcf
{
[ServiceContract]
public interface ImiServicio
{
[OperationContract]
int getSuma(int numero1, int numero2);
}
}
miServicio.svc.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace primerwcf
{
public class miServicio : ImiServicio
{
public int getSuma(int numero1, int numero2)
{
return numero1+numero2;
}
}
}
网络配置
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.6.1" />
<httpRuntime targetFramework="4.6.1"/>
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>
Javascript/Jquery 代码
$("#b1").click(function()
{
$.ajax(
{
type: "GET",
url:"http://localhost:50664/miServicio.svc",
data:{
numero1:2,
numero2:6
},
dataType: "xml" ,
success:function(response)
{
alert("ok "+response);
},
error:function(){
alert("error");
}
});
});
当我简单地展示
alert(response);
它返回一个对象 XMLDocument
但是当我尝试使用
$.parseXML(response);
它返回空值。
我在 ajax() 方法中将数据类型更改为“文本”,但我只获得了粘贴地址时将在浏览器中显示的 HTML 代码。
我也试过这样调用方法:
response.getSumarResult
但它返回未定义。
我调查了这些来源,其中包括:
https://www.codeproject.com/Questions/544312/HowplustoplusreadplusXMLplusFileplusinplusaplusWCF
http://www.developerin.net/a/62-Code-Snippets/49-Calling-WCF-services-using-Jquery
由于引用的大部分教程都是旧的,我不知道我是否必须像一些网站建议的那样配置 webConfig 文件,因为我似乎成功地从服务中获取了数据。
最佳答案
经过多次测试和努力,终于解决了。我会在这里发布我为其他有同样问题的人所做的事情。
注意我使用的是 VS 2017:
-首先,我删除了 ImiServicio.cs 和 miServicio.svc.cs
-选择当前项目/添加新元素,然后包含一个启用了 AJAX 的新 WCF 服务。您不需要以这种方式创建界面。
-在新的 WCF 上,我将这段代码添加到 getSuma() 方法中:
**[WebGet(ResponseFormat = WebMessageFormat.Xml)]**
public int getSuma(int numero1, int numero2)
{
WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";
return numero1 + numero2;
}
方法主体中的“WebOperationContext...”行似乎不是强制性的,但 VS 建议包含它,所以我这样做了。
-现在,我们转到 jQuery 代码并更改这些行:
url:"http://localhost:50664/miServicio.svc"
注意我没有在旧代码上调用方法,所以这样做:
url:"http://localhost:50664/miServicio.svc/getSuma"
-接下来,在成功功能上,替换为:
alert("ok "+ response);
有了这个:
/*use the text() jQuery method to obtain the value returned by the method*/
alert("ok "+$(response).text());
重要提示:我仍然不知道如何通过代码启用 CORS,所以我不得不使用 firefox extension暂时避免这个问题。如果您不启用,尽管响应正常(状态 200),您仍会收到错误消息。
关于javascript - 如何在 jQuery 中使用 WCF 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47088880/