Derik Whittaker

Syndication

News


Help I am getting an XMLHttpRequest exception of Access-Control-Allow-Orig

When working on a demo application recently I was trying to get an ajax call to hit one of my Web API routes and I was immediately hit with the following exception.

XMLHttpRequest cannot load http://localhost:33884/api/ToDo/. Origin http://localhost:3462 is not allowed by Access-Control-Allow-Orig

Of course this sucked because I KNEW what the issue was but not exactly how to solve it.  Well solve it the way I wanted.  Of course i could have just put both my sites (MVC project and WebAPI project) under the same web application in IIS to remove this but that was not an acceptable answer for my needs.

After a bit of searching online I thought I came across an solution that would work.  I thought that I could just set the crossDomain switch in my ajax call as below to get it to work, but no dice.

I found another suggestion telling me to change the dataType of request form json to jsonp as below, but as you may have guessed that did not work.

Finally at a bit more searching I found more information which suggested I needed to add a response header on the server side to allow ‘Access-Control-Allow-Origin’ and this Stack Overflow answer gave me all the details needed to make this happen in Web API.  Once I implemented the solution inside my WebApi endpoints and REMOVED the 2 un-needed attributes in my .ajax call the world was a happy place and I was able to make cross domain calls to my other services.

Till next time,


Posted 02-10-2013 6:34 AM by Derik Whittaker

[Advertisement]

Comments

Angelo wrote re: Help I am getting an XMLHttpRequest exception of Access-Control-Allow-Orig
on 02-10-2013 12:41 PM

I'm a brazilian Asp.Net Developer, and i resolved this problem creating a ActionFilter  to enable Cross-Domain requests.

i hope it useful to you!

tks :-)

pontonetbrasil.wordpress.com/.../resolvendo-o-problema-de-cross-domain-no-asp-net-webapi

Steven wrote re: Help I am getting an XMLHttpRequest exception of Access-Control-Allow-Orig
on 02-11-2013 11:26 AM

I have found the Thinktecture.IdentityModel NuGet package extremely helpful for handling CORS.

Access Control wrote re: Help I am getting an XMLHttpRequest exception of Access-Control-Allow-Orig
on 02-12-2013 3:19 AM

Solutions to improve process efficiency through unique & secure identification of people | Time & attendance, access control & business process efficiency.

<a href="www.biometricidentitymanagement.com ">Access Control</a>

social bookmarks wrote re: Help I am getting an XMLHttpRequest exception of Access-Control-Allow-Orig
on 03-15-2013 8:20 AM

ErBprg This is one awesome article post.Much thanks again. Great.

buy social bookmarks wrote re: Help I am getting an XMLHttpRequest exception of Access-Control-Allow-Orig
on 04-04-2013 2:37 AM

S0ZVKy This is one awesome article.Really looking forward to read more.

comedy channel wrote re: Help I am getting an XMLHttpRequest exception of Access-Control-Allow-Orig
on 04-06-2013 2:40 AM

Thanks so much for the post.Really thank you! Really Cool.

stoner wrote re: Help I am getting an XMLHttpRequest exception of Access-Control-Allow-Orig
on 04-06-2013 7:59 PM

Im obliged for the blog.Really looking forward to read more. Keep writing.

social bookmarking service wrote re: Help I am getting an XMLHttpRequest exception of Access-Control-Allow-Orig
on 04-07-2013 8:03 PM

5TJwZl Thank you ever so for you blog post.Really looking forward to read more. Fantastic.

Social bookmarks wrote re: Help I am getting an XMLHttpRequest exception of Access-Control-Allow-Orig
on 04-12-2013 10:15 AM

aY1iwf Muchos Gracias for your blog article.

buy social bookmarks wrote re: Help I am getting an XMLHttpRequest exception of Access-Control-Allow-Orig
on 04-13-2013 7:34 PM

oyKQFo This is one awesome article post.Thanks Again. Want more.

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)