Anwar Shaikh
Anwar Shaikh

Reputation: 104

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. I cannot control server side response header because its public API .can any body help on this.

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

Upvotes: 0

Views: 2004

Answers (1)

Bill
Bill

Reputation: 25555

You need to set dataType to jsonp in your ajax request.

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,

Upvotes: 1

Related Questions