jquery calls WCF across domains

Source: Internet
Author: User

The following error is reported when invoking a WCF service across domains using jquery

1 $.ajax ({2URL: ' Http://localhost:28207/Service1.svc/GetData ',3Method: ' Get ',4DataType: ' JSON ',  5 data: {value:val},6Successfunction(data) {7$ ("label"). Text ("Success:" +data);8             },9Errorfunction(err) {Ten$ ("label"). Text ("Error:" +err); One             } A});

Previously thought that cross-domain requests can be as long as the caller makes modifications, and when the problem is solved, it is found that the binding configuration of the server-side WCF also needs support

One, WCF service-side configuration
    1. You need to set the WebHttpBinding property crossdomainscriptaccessenabled in the configuration to True

2. The interface has to support get-mode calls because the way jquery requests cross-domain is get

1 [ServiceContract]2      Public InterfaceIService13     {4         //cross-domain calls to support get mode5[WebInvoke (Method = "GET",6Requestformat =Webmessageformat.json,7Responseformat =Webmessageformat.json,8Bodystyle =Webmessagebodystyle.bare,9UriTemplate ="/getdata?value={value}")]Ten         stringGetData (intvalue); One}
Second, the client calls

Called in Jsonp, which indicates a cross-domain request

1    varval = $ ("#txtValue"). Val ();2         //jquery cross-domain call JSONP mode3         //jquery automatically fills in the question mark in callback=?4         //the URL requested at the time of the actual call is Http://localhost:28207/Service1.svc/GetData?callback=jQuery1102023912459355778992_ 1460275935452&value=321&_=14602759354535 6 $.ajax ({7URL: ' Http://localhost:28207/Service1.svc/GetData ',8Method: ' Get ', //This can be removed, because JSONP default is get mode 9DataType: 'Jsonp‘,  Ten data: {value:val}, OneSuccessfunction(data) { A$ ("label"). Text ("Success:" +data); -             }, -Errorfunction(err) { the$ ("label"). Text ("Error:" +err); -             } -});
Iii. Results

You can see that the JSONP cross-domain invocation of jquery automatically adds a callback parameter to the server callback.

Sample code

jquery calls WCF across domains

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.