Jquery ajax CORS cross-origin access to WebService

Source: Internet
Author: User

Jquery ajax CORS cross-origin access to WebService

JS Code:

 

var word = document.getElementById(word).value;$.ajax({    type: POST,    contentType: application/x-www-form-urlencoded,    url: http://localhost:12805/WebService.asmx/HelloWorld,    data: 'data=' + word,    dataType: 'text',    success: function(result) {        alert(success:  + result);    },    error: function(result, status) {        alert(error:  + status);    }});

C # WebService code:

 

 

[WebService (Namespace = weburl)] [WebServiceBinding (ConformsTo = WsiProfiles. basicProfile1_1)] [System. componentModel. toolboxItem (false)] // To allow this Web Service to be called from script, using ASP. net ajax, uncomment the following line. [System. web. script. services. scriptService] public class WebService: System. web. services. webService {[WebMethod] public void HelloWorld (string data) {this. context. response. addHeader (Access-Control-Allow-Origin, *); // CORS cross-Origin Access this. context. response. write (data); // return string format }}

WebService returns the XML format by default, but the preceding method is returned as a string.

 

 

 

 

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.