c# - Cross-domain ajax calls won't work, even though it works in tutorials -
i'm wokring in asp.net. want create cross domain ajax calls webmethod
using cors
.
i've come across many examples on internet, including many tutorials ans questions, somehow none works me.
look @ example, it's 1:1 code tutorial found on matter , basic:
// default.aspx.cs [system.web.services.webservicebinding(conformsto = system.web.services.wsiprofiles.basicprofile1_1)] public class myservice : system.web.services.webservice { [system.web.services.webmethod] [system.web.script.services.scriptmethod(responseformat = system.web.script.services.responseformat.json, usehttpget = false)] public string helloworld(string test) { string result = ""; result = "success"; return result; } } // global.asax httpcontext.current.response.addheader("access-control-allow-origin", "*"); if (httpcontext.current.request.httpmethod == "options") { httpcontext.current.response.addheader("access-control-allow-methods", "get, post"); httpcontext.current.response.addheader("access-control-allow-headers", "content-type, accept"); httpcontext.current.response.addheader("access-control-max-age", "1728000"); }
then try run ajax
call web browser console (which of course cross-domain call):
$.ajax({ url: "http://localhost:53561/myservice.asmx/helloworld", type: "post", contenttype: "application/json; charset=utf-8", data: '{"test":"asdf"}' }).done(function (result) { alert(result.d); }).fail(function (result) { alert(result.d); });
stil, though should work charm (as said on tutorial page), still xmlhttprequest cannot load http://localhost:53561/myservice.asmx/helloworld. no 'access-control-allow-origin' header present on requested resource. origin 'http://www.google.pl' therefore not allowed access.
error in console.
do need antyhing else? should add more code in web.config
or global.asax
? or can there more security reasons why won't work?
Comments
Post a Comment