【问题标题】:Call WCF service from jquery ajax using node js使用节点 js 从 jquery ajax 调用 WCF 服务
【发布时间】: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

【问题讨论】:

    标签: jquery node.js wcf


    【解决方案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。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-27
      • 2014-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-05
      相关资源
      最近更新 更多