3

Seemed to me to be a beaten theme, but i couldn't find the answer. =( I make jquery ajax requst to localhost:666 from localhost:555 application

    $.ajax({
            url: "http://localhost:666/request",
            dataType: 'json',
            timeout: 5000,
            success:...

i've got in chrome:

XMLHttpRequest cannot load http://localhost:666/request. Origin http://localhost:555 is not allowed by Access-Control-Allow-Origin.

What is the solution of the problem?

tereško
  • 57,247
  • 24
  • 95
  • 149
WHITECOLOR
  • 22,508
  • 35
  • 117
  • 177

3 Answers3

8

You can initiate cross-domain request in your webpage by creating either XMLHttpRequest object or XDomainRequest object. End user's web-browser will request data from the domain's server by sending an "Origin" header with the value of origin. If server responds with an "Access-Control-Allow-Origin: * | Origin" then we are permitted to access data; otherwise response will be unauthorized request.

protected void Application_BeginRequest(object sender, EventArgs e)
{
    HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");

    // HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "http://AllowedDomain.com");
}

An article here: Cross-Origin requests and ASP.NET MVC

Muaz Khan
  • 6,850
  • 7
  • 40
  • 76
-1

ajax calls are confined to parent domain only. for this a site on localhost:666 can not open ajax connection to localhost:555 since they belongs to different domain (or origin)

you need to try jsonp: http://www.google.com/search?q=jsonp

seoul
  • 870
  • 1
  • 12
  • 32
-2

Try using dataType: 'jsonp', or $.getJSON function.

ulu
  • 5,514
  • 3
  • 38
  • 50