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.