【问题标题】:I'm getting error while running application in my local host.XMLHttpRequest cannot load在本地主机中运行应用程序时出现错误。无法加载 XMLHttpRequest
【发布时间】:2017-03-02 09:37:01
【问题描述】:

我想从本地主机测试我的页面,其中包含 get api,它是远程服务器中的主机。在执行此页面时,我在控制台中收到此错误。

XMLHttpRequest 无法加载 http://abcsoftwares.in/mymethod/get。请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,Origin 'http://localhost:6366' 不允许访问。 我在这里使用 ajax 调用来获取 json 响应。

    function LoadChart() {
        $.ajax({
            url: 'http://abcsoftwares.in/mymethod/get',
            type: 'GET',
            crossDomain: true,
            success: function (data) {
                if (data.success) {
                    var WaterTemperature = 12;                       
                    var fusioncharts = new FusionCharts({
                        type: 'thermometer',
                        renderAt: 'abcdef',
                        id: 'myThm',
                        width: '240',
                        height: '301',
                        dataFormat: 'json',
                        dataSource: {
                            "chart": {

                            },
                            "value": abc,
                            "annotations": {
                                "showbelow": "0",
                                "groups": [{
                                    "id": "indicator",
                                    "items": [
                                        {
                                            "id": "background",
                                            "type": "rectangle",

                                        }
                                    ]
                                }]

                            },
                        }
                    });

我不想更改 url 以在本地主机中进行测试。

【问题讨论】:

    标签: javascript html asp.net ajax


    【解决方案1】:

    您的服务器应用必须启用 CORS

    在您的 WebApiConfig 中:

    config.EnableCors();
    

    在每个动作中用这个装饰:

    [EnableCors(origins: "*", headers: "*", methods: "*")]
    

    【讨论】:

      【解决方案2】:

      正如给定的错误消息所说:

      请求的资源上不存在“Access-Control-Allow-Origin”标头。 Origin 'http://localhost:6366' 因此不允许访问。

      您不能 GET 或 POST 到不允许您访问的远程。 一种常见的解决方案是向 API 服务器添加标头 Access-Control-Allow-Origin:*

      【讨论】:

        猜你喜欢
        • 2023-04-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-06-25
        • 2016-02-04
        • 2020-11-25
        • 2016-10-26
        • 2018-10-16
        相关资源
        最近更新 更多