【发布时间】:2018-08-12 11:56:07
【问题描述】:
您好,我正在尝试在我的节点 js 应用程序中使用 jquery ajax 调用 WCF 服务。
它不会给出任何错误,但服务没有调用。但是当我在没有节点 js 的情况下尝试这段代码时它可以工作。
如何从节点js调用wcf服务
WCF 服务合同
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebInvoke(Method = "POST",
BodyStyle = WebMessageBodyStyle.Wrapped,
ResponseFormat = WebMessageFormat.Json)]
string GetData(int value);
[OperationContract]
CompositeType GetDataUsingDataContract(CompositeType composite);
// TODO: Add your service operations here
}
WCF 服务
public class Service1 : IService1
{
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
}
Web.config
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="EndpBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
<services>
<service behaviorConfiguration="ServiceBehavior" name="Service1">
<endpoint address="" binding="webHttpBinding" contract="IService1" behaviorConfiguration="EndpBehavior"/>
</service>
</services>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<!--
To browse web app root directory during debugging, set the value below to true.
Set to false before deployment to avoid disclosing web app folder information.
-->
<directoryBrowse enabled="true"/>
</system.webServer>
还有我的 Node js 代码
var jsdom = require("jsdom");
const { JSDOM } = jsdom;
const { window } = new JSDOM();
const { document } = (new JSDOM('')).window;
global.document = document;
var $ = jQuery = require('jquery')(window);
var showStb = function(){
try{
$.ajax({
type: "POST",
url: "http://localhost:56147/Service1.svc/GetData",
data: '{"value": "2"}',
dataType: "jsonp",
contentType: "application/json", // content type sent to server
success: function(data){
console.log(data);
},
error: function(err){
console.log(err);
}
});
}
catch(e){
console.log( e);
}
};
showStb();
我正在关注这个示例 enter link description here
【问题讨论】: