4

Error :

Response to preflight request doesn't pass access control check: The 'Access-Control-Allow-Origin' header contains multiple values 'http://localhost:8080, *', but only one is allowed. Origin 'http://localhost:8080' is therefore not allowed access.

$("#selector3").autocomplete({
            source: function(request, response) {               

                $.ajax({
                   url: "http://example.com/"+$("#selector3").val(),
                    type: "GET",
                        dataType: "json",
                        data: request,
                        processData: true,
                        data: {},

                        headers: { 
                            "Access-Control-Allow-Origin" : "*",
                            "Access-Control-Allow-Headers": "origin, content-type, accept"
                                    },

                        crossDomain: true,
                    success: function(data) {   
                        alert(data.Company_Id);
});

3 Answers 3

2

Please use JSONP for cross domain scripting.

Check below sample code:

$.ajax({
    url: "http://example.com/" + $("#selector3").val(),
    type: "GET",
    dataType: 'jsonp', // Notice! JSONP <-- P (lowercase)
    data: request,
    processData: true,
    data: {},
    headers: {
        "Access-Control-Allow-Origin": "*",
        "Access-Control-Allow-Headers": "origin, content-type, accept"
    },
    success: function(data) {
        alert(data.Company_Id);
    }
});
Sign up to request clarification or add additional context in comments.

3 Comments

I have changed datatype : jsonp, I get response error in ( Error: jQuery1113042518210380525434_1461646502000 was not called )
@Arunkumar, That means either a network error or an end point that doesn't return a JSONP response.
Not a real solution if you set a bearer token or any header parameter, headers is totally ignored
0

The headers you send to the server from Javascript, should be returned by the server. If you have no access to the server this will be impossible. This means the server accepts your domain as a legal processor.

Next to that you can try a JSONP call. Check this URL: https://learn.jquery.com/ajax/working-with-jsonp/

Comments

-1

you can't do ajax operaitons on other websites source code except you have rights. you have to to that on the server side. in php you can use cURL, in .net you can use html agility pack.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.