嗨,我正在尝试在我的节点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
答案 0 :(得分:1)
假定您的服务托管正确。让我尝试重新排序并向您解释每个属性。
$.ajax({
//where is thre url present
url: "http://localhost:56147/Service1.svc/GetData",
//what kind of request
method: "post",
//contenttype that we will send it to the server
contentType: "application/json;charset-utf-8",
//lets convert the data to json and send it to the server
data: JSON.stringify({value: "2"}),
dataType: "json",
//when the request is success
success: function(data){
console.log(data);
},
//if any error
error: function(err){
console.log(err);
}
});
这应该可以完成工作,而不是使用jsonp。