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();
       type: "POST",
       url: "http://localhost:8000/project/login/uid=" + username + ";token=" + token,
       success: function (html) {



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:

or, specifically for django:

Wednesday, March 31, 2021
answered 7 Months ago

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

$serverName = ", 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:

Wednesday, March 31, 2021
answered 7 Months ago

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
answered 7 Months ago

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

    var token = '{{csrf_token}}';

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

Saturday, August 14, 2021
answered 3 Months ago

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
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 :