本文介绍了使用jquery调用IIS中托管的WCF服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个WCF服务并在IIS中托管。



WCF服务 -





I have created a WCF service and hosted in IIS.

WCF service--


 [ServiceContract]
 public interface IService1
 {
     [OperationContract]
     [WebInvoke(Method = "GET",ResponseFormat = WebMessageFormat.Json)]
     string GetData(int value);

     [OperationContract]
     [WebInvoke(Method ="POST", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json)]
     string[] GetUser(string Id);

 }


[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
 public class Service1 : IService1
 {
     public string GetData(int value)
     {
         return string.Format("You entered: {0}", value);
     }


     public string[] GetUser(string Id)
     { return new User().GetUser(Convert.ToInt32(Id)); }
 }





WCF的Web配置 -





Web Config of WCF--

<?xml version="1.0"?>
<configuration>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="mexBehavior">
          <serviceMetadata httpGetEnabled="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service behaviorConfiguration="mexBehavior" name="DemoService.Service1">
        <endpoint address="DemoSevice" binding="webHttpBinding" bindingConfiguration="" contract="DemoService.IService1"/>
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8090"/>
          </baseAddresses>
        </host>
      </service>
    </services>
  </system.serviceModel>
  <system.web>
    <compilation debug="true"/>
  </system.web>
</configuration>





之后我托管了这个IIS中的服务现在我想使用J Query来使用这个服务。

J查询代码 -



after that I hosted this service in IIS and now I want to use this service using J Query.
J query code--

<script src="js/jquery-1.8.2.min.js" type="text/javascript"></script>

     <script type="text/javascript">

         var Type;
         var Url;
         var Data;
         var ContentType;
         var DataType;
         var ProcessData;
         //Generic function to call AXMX/WCF  Service        
         function CallService() {
             debugger;
             $.ajax({
                 type: Type, //GET or POST or PUT or DELETE verb
                 url: Url, // Location of the service
                 data: Data, //Data sent to server
                 contentType: ContentType, // content type sent to server
                 dataType: DataType, //Expected data format from server
                 processdata: ProcessData, //True or False
                 dataFilter: function (data) { return data; },
                 success: function (msg) {
                     debugger;
                     var jsonData = jQuery.parseJSON(msg);
                     ServiceSucceeded(jsonData);
                 },

                 error: ServiceFailed// When Service call fails
             });
         }

         function ServiceFailed(result) {
             alert('Service call failed: ' + result.status + '' + result.statusText);
             Type = null; Url = null; Data = null; ContentType = null; DataType = null; ProcessData = null;
         }

         function WCFJSON() {
             var uesrid = "1";
             Type = "POST";
             Url = "http://localhost:8090/DemoService/Service1.svc/GetUser";
             Data = '{"Id": "' + uesrid + '"}';
             ContentType = "application/json; charset=utf-8";
             DataType = "json";
             ProcessData = true;
             CallService();
         }

         function ServiceSucceeded(result) {

             if (DataType == "json") {

                 resultObject = result.GetUserResult;

                 for (i = 0; i < resultObject.length; i++) {
                     alert(resultObject[i]);
                 }

             }

         }

         function ServiceFailed(xhr) {
             alert(xhr.responseText);
             if (xhr.responseText) {
                 var err = xhr.responseText;
                 if (err)
                     error(err);
                 else
                     error({ Message: "Unknown server error." })
             }
             return;
         }

         $(document).ready(
         function () {
             WCFJSON();
         }
         );

    </script>





但是这个j查询块在我的结尾不起作用。我在哪里做错了?任何想法将不胜感激。



But this block of j query is not working in my end. Where I am doing mistake?. Any idea will be appreciated.

推荐答案






但是这个j查询块在我的结尾不起作用。我在哪里做错了?任何想法都将不胜感激。



But this block of j query is not working in my end. Where I am doing mistake?. Any idea will be appreciated.


这篇关于使用jquery调用IIS中托管的WCF服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 09:02
查看更多