Asked  7 Months ago    Answers:  5   Viewed   47 times

When I'm trying to make an AJAX call from php (localhost) to django (localhost:8000), it throws the following error.

XMLHttpRequest cannot load http://localhost:8000/project/login/uid=bimt;token=KAMWMS151UWP67Q. Origin http://localhost is not allowed by Access-Control-Allow-Origin.

$(document).on('click', '.login', function(event) {
    var username = $('#username').val();
    var token = $('#token').val();
    $.ajax({
       type: "POST",
       url: "http://localhost:8000/project/login/uid=" + username + ";token=" + token,
       success: function (html) {
         alert(html);
        }
     });
});

 Answers

52

Because the port is not the same, it's considered a cross origin request. You must set a Access-Control-Allow-Origin header in the script you are requesting.

Learn more:

http://enable-cors.org/server.html

or, specifically for django:

http://chase-seibert.github.io/blog/2012/01/27/using-access-control-allow-origin-to-make-cross-domain-post-requests-from-javsacript.html

Wednesday, March 31, 2021
 
Easen
answered 7 Months ago
23

you have to use the SQL Server native driver for php at first place, then you can do something like:

$serverName = "tcp:sample.database.windows.net, 1433";

$connectionOptions = array("Database" => "sampleInit", 

                           "UID" => "sampleUsr@sample",

                           "PWD" => "samplePass",

                           "MultipleActiveResultSets" => false);

$conn = sqlsrv_connect($serverName, $connectionOptions);

if($conn === false)

{

     die(print_r(sqlsrv_errors(), true));

}

You can read more on PHP and SQL Azure at following blog post:
http://blogs.msdn.com/b/brian_swan/archive/2010/02/12/getting-started-with-php-and-sql-azure.aspx

Wednesday, March 31, 2021
 
MGP
answered 7 Months ago
MGP
10

Ok, there is a kind of solution, I do not really like it, but it is something instead of nothing.

The idea is to use more smart "sleep", there is a method waitUntil() which takes an anonymous function and timeout in milliseconds. What is does - runs this passed function in loop until timeout hits or your function return True. So you can run something and wait until context is changed:

$this->waitUntil(function () {
    if ($this->byCssSelector('h1')) {
        return true;
    }
    return null;
}, 5000);

I still will be glad if somebody give better solution.

Wednesday, March 31, 2021
 
EurekA
answered 7 Months ago
86

See below for how I changed your code. The csrf_token is assigned to a variable with Django templating. You can produce this variable in any of your Javascript code.

The token is then included in the header

 <script>
    var token = '{{csrf_token}}';

    $("#id_username").change(function () {
      console.log($(this).val());
      var form = $(this).closest("form");
      $.ajax({
        headers: { "X-CSRFToken": token },
        url: form.attr("data-validate-username-url"),
        data: form.serialize(),
        dataType: 'json',
        success: function (data) {
          if (data.is_taken) {
            alert(data.error_message);
          }
        }
      });

    });
  </script>
Saturday, August 14, 2021
 
Thermatix
answered 3 Months ago
69

Try adding a timestamp to your request on the handheld, it did the trick for me back then, and I think Google updated their documentations adding this bit of info

Saturday, August 21, 2021
 
Rabbid76
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :