ajax function does not work

0

I'm using this little function to run my ajax without mimimi, see

var HTTP = 'http://127.0.0.1/';
function ajax(to, data) {
       $.ajax({
type: "POST",
url: (HTTP + to),
data: data, 
cache: false,
success: function(output) {
alert(output);
}
});
    return false;
};
ajax('ds', null);

but it does not run successfully nor does water occur, the problem is in the URL but I do not know how to solve it: /

    
asked by anonymous 22.03.2015 / 04:20

1 answer

1

Try removing the link and just use the relative url, like this:

var HTTP = '/'; or var HTTP = '/index.php';


Is this a bug?

No, this is because of CORS . This functionality prevents AJAX requests from being made to URLs that are not the same as your page.

So by using http:// you defined that you would not be pointing to a URL at the same address as the one on the page (127.0.0.1)


How do I solve it?

For the page (s) that will respond to your request, add CORS headers .

I do not know what server you are using, but if you have PHP support you can add the headers as follows in index.php

<?php
header("Access-Control-Allow-Origin: *"); // Aceitar requisições de qualquer endereço

Or even

<?php
header("Access-Control-Allow-Origin: meusite.com"); // Aceitar requisições AJAX de meusite.com


Reference & More on the subject

There are many options for CORS and so I will not name them all. My source is below.

Source: link (in english )

    
22.03.2015 / 05:04