0

I am trying to do an auto search for my html form. I requesting to controller for data but it is saying:

Failed to load http://127.0.0.1:8080/Project/abc/abc_foo/foocon/: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access.

I added following code to my controller:

Header('Access-Control-Allow-Methods', '*');

Header('Access-Control-Allow-Headers', '*');

Header('Access-Control-Allow-Credentials','*');

Please find the ajax code below:

$(document).ready(function () {
    $("#inputName").keyup(function(){
    var valueCount = $("#inputName").val();
        if(valueCount.length > 3){
            $.ajax({
                url: 'http://127.0.0.1:8080/Project/abc/abc_foo/foocon/',
                dataType: 'json',
                type: 'post',
                contentType: 'application/json',
                data: JSON.stringify({ "inputName": $('#inputName').val()}),
                processData: false,
                success: function (data, textStatus, jQxhr) {
                    console.log(data);
                },
                error: function (jqXhr, textStatus, errorThrown) {
                    console.log(errorThrown);
                }
            });
        }
    });
});

Please suggest the solution. I checked several times but not getting any help.

0

You can manually set the header in the ajax call:

$.ajax({
     url: "http://your-url...",
 headers: {'X-Requested-With': 'XMLHttpRequest'}
});  

or you can set not to use cross-domain defaults, so it will keep the X-Requested-With header in the ajax request:

$.ajax({
  url: "http://your-url...",
 crossDomain: false
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.