我在本地host中运行应用程序时遇到错误.XMLHttpRequest无法加载

时间:2016-10-20 11:45:41

标签: javascript html asp.net ajax

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

XMLHttpRequest无法加载http://abcsoftwares.in/mymethod/get。请求的资源上不存在“Access-Control-Allow-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",

                                        }
                                    ]
                                }]

                            },
                        }
                    });

我不想更改本地主机测试的网址。

2 个答案:

答案 0 :(得分:0)

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

否'访问控制 - 允许 - 来源'标头出现在请求的资源上。起源' http://localhost:6366'因此不允许访问。

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

答案 1 :(得分:0)

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

在您的WebApiConfig中:

config.EnableCors();

用每个动作装饰:

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