简体   繁体   English

Access-Control-Allow-Origin不允许来源http:// localhost:8080

[英]Origin http://localhost:8080 is not allowed by Access-Control-Allow-Origin

Hi there are already lots of threads related to this problem. 嗨,已经有很多与此问题相关的线程。 But still i got stuck into the same . 但是我仍然陷入同样的​​困境。 When I am trying to access this REST api, I couldnt able to get the response. 当我尝试访问此REST API时,无法获得响应。 I cannot control server side response header because its public API .can any body help on this. 我无法控制服务器端响应标头,因为它的公共API可以对此提供任何帮助。

Below is my code 下面是我的代码

$(document).ready(function() {
    $("#medication").autocomplete({
        select : function(request, response) {
        //$(this).value(response.item.value);
        getMedicationIdByName(response.item.value);
    },
    search  : function(){$(this).addClass('working');},
    open    : function(){$(this).removeClass('working');},

    source : function(request, response) {
        $.ajax({

            headers : {
                        Accept : "application/json; charset=utf-8",
                        "Content-Type" : "application/json; charset=utf-8"
            },
            type : "GET",
            url : "http://rxnav.nlm.nih.gov/REST/spellingsuggestions",
            data : "name="+ request.term,
            crossDomain: 'true',
            dataFil ter : function(data) {
                return data;
            },

            success : function(data) {
                try {
                        //alert("success!!"+JSON.stringify(data));

                        data = data.suggestionGroup.suggestionList["suggestion"]+ "";
                        data = data.split(",");

                        response($.map(data,function(item) {
                            //alert("success!!"+JSON.stringify(item))
                            return {
                                    value : item
                            };
                        }));
                } catch (e) {
                    alert(e);
                }

            },
            error : function() {
                alert('Cannot connect to REST API!!!');
            }
        });
    },
    minLength : 4
    });
});

You need to set dataType to jsonp in your ajax request. 您需要在ajax请求中将dataType设置为jsonp

type: "GET",
headers: { 
    Accept : "application/json; charset=utf-8",
    "Content-Type": "application/json; charset=utf-8"
},
url: "http://rxnav.nlm.nih.gov/REST/spellingsuggestions",
data: { name: "bard"},
dataType: "jsonp",
crossdomain: true,

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法加载资源:Access-Control-Allow-Origin不允许使用原始http:// localhost:8080 - Failed to load resource: Origin http://localhost:8080 is not allowed by Access-Control-Allow-Origin Access-Control-Allow-Origin不允许AngularJS Origin http:// localhost:8080 - AngularJS Origin http://localhost:8080 is not allowed by Access-Control-Allow-Origin Access-Control-Allow-Origin不允许使用origin http:// localhost - Origin http://localhost is not allowed by Access-Control-Allow-Origin 所请求的资源上没有“ Access-Control-Allow-Origin”标头。 因此,不允许访问来源“ http:// localhost” - No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access XMLHttpRequest无法加载http:// localhost:8089 / jquery。 Access-Control-Allow-Origin不允许使用原点null - XMLHttpRequest cannot load http://localhost:8089/jquery. Origin null is not allowed by Access-Control-Allow-Origin AngularJS MySQL REST - Access-Control-Allow-Origin不允许使用原始http:// localhost:8383 - AngularJS MySQL REST - Origin http://localhost:8383 is not allowed by Access-Control-Allow-Origin Google的Places API和JQuery请求-Access-Control-Allow-Origin不允许来源http:// localhost - Google's Places API and JQuery request - Origin http://localhost is not allowed by Access-Control-Allow-Origin 使用xui.js的Access-Control-Allow-Origin不允许使用原始http:// localhost - Origin http://localhost is not allowed by Access-Control-Allow-Origin with xui.js javascript - Access-Control-Allow-Origin不允许使用Origin http://127.0.0.1 - javascript - Origin http://127.0.0.1 is not allowed by Access-Control-Allow-Origin Access-Control-Allow-Origin 不允许 Origin - Origin is not allowed by Access-Control-Allow-Origin
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM