Call Wcf service with Javascript in Html page

前端 未结 1 1814
清酒与你
清酒与你 2020-12-29 00:10

I have a WCF project in VSStudio2012 and I want to call a method from JavaScript function.

JavaScript file :

var url = \'http://localhost:52768/Serv         


        
1条回答
  •  囚心锁ツ
    2020-12-29 00:36

    Based on this article, the issue is that AJAX has cross-site limitation which prevents you to call remote service. For such cases, a simple workaround is define a server-side page_method (script callback) or local wcf service within the same application which use server-side code to call the remote WCF service. And your web page use AJAX to call the local WCF service (which works like an intermediary).

    Another approach is defining your remote WCF service as a standard REST service which accept http GET request. Thus, your web page can use JQuery api to access the remote WCF service operation through JQuery script. You then host your WCF service as a console application and use JQuery in another web application to call it:

      [ServiceContract(Namespace="ConsoleAJAXWCF")]
      public interface IService1
      {
        [OperationContract]
        [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json)]
        string GetTEST();
      }
    
      [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
      public class Service1 : IService1
      {
    
        public string GetTEST()
        {
          return "OKKKKKKKK";
        }
      }
    

    You hosting console application:

    // program.cs
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.ServiceModel;
    using System.ServiceModel.Description; 
    using ConsoleAJAXWCF;    
    
    namespace ConsoleAJAXWCF
    {
        class Program
        {
            static void Main(string[] args)
            {
    
                // Step 1 Add a service endpoint.
                using (WebServiceHost selfHost = new WebServiceHost(typeof(Service1)))
                {
                    try
                    {
                       // Step 2 Start the service.
                       selfHost.Open();
                       Console.WriteLine("The service is ready.");
                       Console.WriteLine("Press  to terminate service.");
                       Console.WriteLine();
                       Console.ReadLine();
    
                       // Close the ServiceHostBase to shutdown the service.
                       selfHost.Close();
                    }
                    catch (CommunicationException ce)
                    {
                        Console.WriteLine("An exception occurred: {0}", ce.Message);
                        selfHost.Abort();
                    }                
                }            
            }
        }
    }
        // WCF Configuration    
       
          
           
           
         
        
        
          
            
              
                
              
            
            
              
                
              
            
          
        
    

    Verify the service is working:

    1. Run the ConsoleAJAXWCF console application from inside Visual Studio 2012. When running on Windows Vista and later operating systems, the service must be run with administrator privileges. Because Visual Studio was run with Administrator privileges, GettingStartedHost is also run with Administrator privileges. You can also start a new command prompt running it with Administrator privileges and run service.exe within it.
    2. Open Internet Explorer and browse to the service's debug page at localhost:52768/Service1/

    Code in ASPX page which calls the service:

    
    
       
          
          
          
          
          
          TESTE
       
       
           
       
       
    
    

    0 讨论(0)
提交回复
热议问题