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
- 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