Notes for calling WCF using jquery

Source: Internet
Author: User

Yesterday, the blog garden homepage added the Digg function. In this function, we started to use jquery to directly call WCF. Previously, we used jquery to call web service and WebService to call the service layer. This is mainly because you need to call the WCF Service under different domain names before, because of cross-origin call problems, you need to use web service to transfer. This Digg function calls the same application.ProgramUsing jquery to directly call WCF is a better choice. Some problems encountered during the process of trying this method and some notes need to be recorded, so I wrote this article.

Xland's jquery calling WCF has helped us a lot. Thanks to xland! In the process of exploring technology, you can record your own experience in solving problems, not only to be forgotten and summarized, but also to help friends who encounter the same problem, this is also a pleasure to write a blog.

To get started, jquery should pay attention to the following issues when calling WCF:

1. Configure WCF(The WCF Service is hosted on IIS 7)

1) configure the WCF Service:

Add the attribute [webinvoke (requestformat = webmessageformat. JSON, responseformat = webmessageformat. JSON, bodystyle = webmessagebodystyle. wrappedrequest) to the interface method to be called, for example:

[Servicecontract]
Public   Interface Idiggservice
{
[Operationcontract]
[Webinvoke (requestformat = Webmessageformat. JSON, responseformat = Webmessageformat. JSON, bodystyle = Webmessagebodystyle. wrappedrequest)]
String Getdiggcountlist ( String Entryidlist );
}

Add attributes to the service implementation class:

  [Aspnetcompatibilityrequirements (requirementsmode = Aspnetcompatibilityrequirementsmode. Allowed)]
Public   Class Diggservice: idiggservice
{
}

Otherwise, an error occurs: "IIS 7.0 detailed error-500.0-system. servicemodel. serviceactivationexception ".

2) Configure WCF in Web. config:

< System. servicemodel >
< Services >
< Service Name = "Diggservice" >
< Endpoint Address = "" Binding = "Webhttpbinding" Contract = "Idiggservice" Behaviorconfiguration = "Diggservicebehavior" />
</ Service >
</ Services >
< Behaviors >
< Endpointbehaviors >
< Behavior Name = "Diggservicebehavior" >
< Enablewebscript />
</ Behavior >
</ Endpointbehaviors >
</ Behaviors >
</ System. servicemodel >

Note the following configurations:

A) binding = "webhttpbinding", start to set it to binding = "basichttpbinding", and the error message is displayed:

The endpoint at 'diggservice. svc' does not have a binding with the none messageversion. 'System. servicemodel. description. webscriptenablingbehavior 'is only intended for use with webhttpbinding or similar bindings.

B) <enablewebscript/>. enable this setting so that WCF can support Ajax calls. Otherwise, WCF will return the following error during the call:

The message with to 'diggservice. svc/getdiggcountlist 'cannot be processed at the specified er, due to an addressfilter mismatch at the endpointdispatcher. Check that the sender and caller's endpointaddresses agree.

Ii. Client jquery call notes

Start calling by calling Web servcie:

$. Ajax ({
URL:
' /WCF/diggservice. svc/getdiggcountlist ' ,
Data:
' {"Entryidlist ":" '   + Entryidlist +   ' "} ' ,
Type:
' Post ' ,
Datatype:
' JSON ' ,
Contenttype: ' Application/JSON; charset = utf8 ' ,
Success:
Function (Data ){
If (Data. d ){
}
}
},
Error:
Function (Xhr ){
Alert (xhr. responsetext );
}
});

It can be successfully called in Firefox, but in IE 8 and Google Chrome, the returned error message is IIS 7: IIS 7.0 detailed error-400.0-bad request.

Later, change contenttype: 'application/JSON; charset = utf8'Contenttype: 'text/json'The problem is solved.

There is another difference between jquery's Web service calling and WCF in parameter format processing:

For exampleCodeData: '{"entryidlist": "' + entryidlist + '"}' in. If you remove double quotation marks from the parameter name, that is, data: '{entryidlist: "'+ entryidlist +'"} '. You can call the Web Service normally. If you call WCF, an JSON deserialization exception occurs.

3. Other notes

If multiple domain names are bound to the IIS Site of the WCF Service, the following error occurs if baseaddressprefixfilters is not set:

This collection already contains an address with scheme HTTP. There can be at most one address per scheme in this collection. Parameter Name: item

Set baseaddressprefixfilters, for example:

< Servicehostingenvironment >
< Baseaddressprefixfilters >
< Add Prefix = "Http://www.cnblogs.com" />
</ Baseaddressprefixfilters >
</ Servicehostingenvironment >

This can be called normally when you access the http://www.cnblogs.com, but an error occurs when you access the http://cnblogs.com (IIS 7.0 detailed error-404.0-not found) because the previous code used a relative address call, the actual call is done (http://www.cnblogs.com/wcf/DiggService.svc/GetDiggCountList), which causes cross-origin calls. I do not know how to solve this problem.

Iv. Legacy issues

How to control the cache, for example, how to set the Expires header and if-modified-since when the WCF returns to avoid frequent WCF calls.

V. Summary

Key points for jquery to call WCF:

1.[Webinvoke (requestformat = webmessageformat. JSON, responseformat = webmessageformat. JSON, bodystyle = webmessagebodystyle. wrappedrequest)]

2. [aspnetcompatibilityrequirements (requirementsmode = aspnetcompatibilityrequirementsmode. Allowed)]

3. Binding = "webhttpbinding"

4. <enablewebscript/>

5. contenttype: 'text/json'

ReferenceArticle:

1. call WCF using jquery

2. jquery Ajax full Parsing

Related teams:Http://space.cnblogs.com/group/100319/

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.