0

I'm getting the error

“No 'Access-Control-Allow-Origin' header is present on the requested resource”

When I try to do this in jQuery

    $.ajax({
        method: "GET",
        url: "http://localhost:52930/api/person"
    })
        .done(function( msg ) {
            alert(  msg );
        });

To a ASP script that is running on port 52930. The url returns a JSON array. Why does it give this error and how do I solve it?

Sinan Samet
  • 6,432
  • 12
  • 50
  • 93
  • Search for *Asp.Net CORS* – DontVoteMeDown May 16 '17 at 16:52
  • I enabled CORS in all controllers now but it still gives the same error. Do I need to change anything on the jquery side? – Sinan Samet May 16 '17 at 17:05
  • [Perhaps using `jsonp`](http://stackoverflow.com/a/5473299/1267304). – DontVoteMeDown May 16 '17 at 17:07
  • I'll try that in a bit but is this right when I enable it for all controllers in WebApiConfig? `var corsAttr = new EnableCorsAttribute("http://localhost:52930", "*", "*"); config.EnableCors(corsAttr);` – Sinan Samet May 16 '17 at 17:16
  • I also tried using the websites origin which was just `http://127.0.0.1` but still same thing – Sinan Samet May 16 '17 at 17:20
  • Using jsonp gives me this error instead: `jquery.min.js:4 GET http://localhost:52930/api/person?callback=jQuery2140005036372845720116_1494955262090&_=1494955262091 500 (Internal Server Error)` – Sinan Samet May 16 '17 at 17:21
  • 1
    It's working now! The problem after enabling CORS was that I didn't have data in the database. Thanks a lot! – Sinan Samet May 16 '17 at 17:58
  • http://stackoverflow.com/questions/35304817/basic-ajax-request-gets-no-access-control-allow-origin-header-is-present-on-t/ – Ramesh Kumar May 22 '17 at 12:13

0 Answers0