Jquery Ajax & WCF

Source: Internet
Author: User

Http://www.cnblogs.com/dudu/archive/2011/01/19/1939094.html

WCF contract

Using system. servicemodel;

Using system. servicemodel. activation;

...

 

[Servicecontract]

[Aspnetcompatibilityrequirements (requirementsmode = aspnetcompatibilityrequirementsmode. Allowed)]

Public class helloservice

{

[Operationcontract]

Public String say (string word)

{

Return string. Format ("You say \" {0} \ ".", word );

}

}

It is worth noting that aspnetcompatibilityrequirements does not take ASP if this setting is not performed. net pipeline, Asp.. net, for example, httpcontext. current is null. The most common scenario is to obtain user logon information based on httpcontext and verify user permissions. Corresponding to this setting, in

<Servicehostingenvironment aspnetcompatibilityenabled = "true">

Web. config

<System. servicemodel>

<Servicehostingenvironment aspnetcompatibilityenabled = "true"> </servicehostingenvironment>

</System. servicemodel>

* Note: After the preceding settings are added to Web. config, The aspnetcompatibilityrequirements attribute must be set for all WCF Service implementations.

 

<System. servicemodel>

<Servicehostingenvironment aspnetcompatibilityenabled = "true">

<Serviceactivations>

<Add relativeaddress = "helloservice. SVC" service = "jquerywcfdemo. helloservice"

Factory = "system. servicemodel. Activation. webscriptservicehostfactory"/>

</Serviceactivations>

</Servicehostingenvironment>

</System. servicemodel>

System. servicemodel. Activation. webscriptservicehostfactory

In a hosting host environment where the service can be dynamically activated to respond to incoming messages, you can automatically add ASP. NET Ajax endpoints to the service without configuration.

Relativeaddress is the address name of the WCF Service, and the service is the helloservice class name created earlier. Factory is the key. It supports Ajax calling and relies on system. servicemodel. Activation. webscriptservicehostfactory.

Default. aspx

<SCRIPT type = "text/JavaScript">

Function submit_word (){

$. Ajaxsettings. url = 'helloservice. svc/write ';

$. Ajaxsettings. type = 'post ';

$. Ajaxsettings. datatype = 'json ';

$. Ajaxsettings. contenttype = 'application/json ';

$. Ajaxsettings. Data = '{"word": "Hello, word "}';

$. Ajaxsettings. Success = function (data ){

Alert (data. D );

};

$. Ajax ();

};

</SCRIPT>

<Input id = "showmsg" type = "button" value = "show" onclick = "submit_word ()"/>

Related Article

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.