繁体   English   中英

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

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

嗨,已经有很多与此问题相关的线程。 但是我仍然陷入同样的​​困境。 当我尝试访问此REST API时,无法获得响应。 我无法控制服务器端响应标头,因为它的公共API可以对此提供任何帮助。

下面是我的代码

$(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
    });
});

您需要在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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM