?Briella 2015-05-30 08:03 采纳率: 0%
浏览 281

如何启用CORS?

I'm making a request from client side to a web-API on different domain to extract data in JSON format. How do I enable Cross Origin Resource Sharing(CORS)? Client runs on https while my web-API runs on http.

This is the AJAX call that I'm making :

$(document).ready(function () {
    $.ajax({
        type: "GET",
        url: "http://map.techriff.in/api/values",
        success: function (json) {
            console.log(json);
        },
        error: function (err) {
            console.log(err);
        }
    });
});
  • 写回答

4条回答 默认 最新

  • weixin_33695450 2015-05-30 22:52
    关注

    You need to add the Access-Control-Allow-Origin: http://domain.com to your response header, where domain.com is replaced with the domain you want to allow (don't use * wildcards).

    How you do this depends one your server stack. In ASP.NET:

    Response.AppendHeader("Access-Control-Allow-Origin", "http://domain.com");
    

    You then need to set $.support.cors = true in your jQuery to enable it on the client.

    评论

报告相同问题?