Home > Uncategorized > Enabling #CORS on #ASMX .NET #Webservices

Enabling #CORS on #ASMX .NET #Webservices


If you would like your clients / users interface with your ASMX .NET webservice directly from Javascript, then you should enable CORS, to allow them do so without using server-side code.

To do so, you add a Global.asax file and add the code;

protected void Application_BeginRequest(object sender, EventArgs e)
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, it’s possible for your clients to use Javascript code (with JQuery) such as the following to interface directly with your webservice:
(This code example is for AvatarAPI.com )

function init()
if (profile.Valid == “false”)
profile.Image = “https://t4.ftcdn.net/jpg/01/30/78/93/240_F_130789368_w0TYy56XPpuDn0pfgXvgG3ZmfWUZenCQ.jpg”;
profile.Name = “Anonymous”

function AvatarAPI(email,callback)
var username = ‘xxxxxx’;
var password = ‘xxxxxxx’;
var strUrl = “https://www.avatarapi.com/avatar.asmx/GetProfile?”;
strUrl += “email=” + email;
strUrl += “&username=” + username;
strUrl += “&password=” + password;
var xml = $(data);
var profile = {
Name : xml.find(“profile”).find(“Name”).text(),
Image : xml.find(“profile”).find(“Image”).text(),
Valid : xml.find(“profile”).find(“Valid”).text(),
IsDefault : xml.find(“profile”).find(“IsDefault”).text()

Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: