WCF Cross Domain Using Jsonp Error Uncaught SyntaxError: Unexpected token :

前端 未结 1 1632
渐次进展
渐次进展 2021-01-29 08:42

I am trying to call webservice over cross domain using jQuery.

Here is my code to call service

 $(document).ready(function () {
        $.ajax({
                 


        
1条回答
  •  孤独总比滥情好
    2021-01-29 09:11

    Your WCF server should be configured to enable jsonp with CrossDomainScriptAccessEnabled

    Here is a simple working example

    Server:

    Task.Run(() =>
    {
        var uri = new Uri("http://0.0.0.0/test");
        var type = typeof(TestService);
        WebServiceHost host = new WebServiceHost(type, uri);
        WebHttpBinding binding = new WebHttpBinding();
        binding.CrossDomainScriptAccessEnabled = true;
        host.AddServiceEndpoint(type, binding, uri);
    
    
        host.Open();
    
    });
    
    [ServiceContract]
    public class TestService
    {
        [OperationContract]
        [WebGet(ResponseFormat = WebMessageFormat.Json)]
        public string Hello()
        {
            return "Hello World";
        }
    }
    

    And this is the javascript code

    
    
    
    
    
    
    

    Now you can test it from your browser

    http://localhost/test/hello
    

    and

    http://localhost/test/hello?callback=myfunc
    

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