javascript-调用WCF方法
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript-调用WCF方法,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2812字,纯文字阅读大概需要5分钟。
内容图文
我有WCF服务
ITourService.cs
namespace Service
{
[ServiceContract]
public interface ITourService
{
[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Xml)]
double?[] GetPoints(string tourname);
}
}
Web.config
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<services>
<service name="TourService.TourService">
<endpoint binding="webHttpBinding" contract="TourService.ITourService" behaviorConfiguration="webHttp"/>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="webHttp">
<webHttp helpEnabled="true"/>
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
因此,我使用jQuery 1-6-2.min.js并尝试调用方法.
函数Start()启动呼叫
var varType;
var varUrl;
var varData;
var varContentType;
var varDataType;
var varProcessData;
function CallService() {
$.ajax({
type : varType, //GET or POST or PUT or DELETE verb
url : varUrl, // Location of the service
data : varData, //Data sent to server
contentType : varContentType, // content type sent to server
dataType : varDataType, //Expected data format from server
processdata : varProcessData, //True or False
success : function(msg) {//On Successfull service call
ServiceSucceeded(msg);
},
error: ServiceFailed// When Service call fails
});
}
function Start() {
varType = "POST";
varUrl = "http://localhost:1592/TourService.svc/GetPoints/";
varData = '{tourname:customname}'; //tourname=customname doesn't works too and many other variants
varContentType = "application/json; charset=utf-8";
varDataType = "xml";
varProcessData = false;
CallService();
}
function ServiceSucceeded(result) {
alert(result);
}
function ServiceFailed(result) {
alert('Service call failed: ' + result.status + ' ' + result.statusText);
varType = null;
varUrl = null;
varData = null;
varContentType = null;
varDataType = null;
varProcessData = null;
}
但是出了点问题.
我有一条消息:服务呼叫失败:0错误
WCF服务工作正常,我在C#上使用Standart客户端对其进行了检查
我认为从javascript到WCF的访问存在错误,但是我不知道在哪里.
解决方法:
您已根据W3C标准实施了一项Web服务,即您的Web服务希望以XML发出请求,并以XML响应.但是,在Javascript代码中,您将使用JSON数据创建REST请求.
当您监视浏览器与服务之间的通信时,您可能会看到该服务的回答为“ 404 Not found”,因为它将仅在URL http://localhost:1592/TourService.svc而不在http://localhost:1592/TourService.svc/GetPoints/上回答.
因此,要前进,您需要将Web服务转换为REST服务.我还建议不仅将JSON用于请求,还应将JSON用于响应.
您可以在此answer中找到使用WCF构建的简单REST服务的示例.
内容总结
以上是互联网集市为您收集整理的javascript-调用WCF方法全部内容,希望文章能够帮你解决javascript-调用WCF方法所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。