1

I'm sending an ajax request to a php API that only allows POST requests.

  $.ajax({
        url: '"//localhost/api/vs_1_0/availability"',
        data: $("#api-test").serialize(),
        type: "POST",
        dataType: "json",
        headers: {
            "PHP_AUTH_USER": "username",
            "PHP_AUTH_PW": "123"
        },
        success: function (data)
        {
            alert("Data from Server" + JSON.stringify(data));
        },
        error: function (jqXHR, textStatus, errorThrown)
        {
            alert("You can not send Cross Domain AJAX requests: " + errorThrown);
        }
    });

when i'm sending this request i can see "Cross-Origin Request Blocked: " error in firebug and it won't execute ajax request. I have to send the username and password inside the header for $_SERVER['PHP_AUTH_PW'] and $_SERVER['PHP_AUTH_USER'] variables. It worked by doing a CURL request. But can i send a request to the server via Ajax request. How can i perform this kind of request.

rzshss
  • 95
  • 1
  • 15
  • I think this can help you: http://stackoverflow.com/questions/23607901/cross-origin-request-blocked-on – Mihai Zinculescu Jun 01 '15 at 10:07
  • As my API is in another domain, this solution wouldn't helped me. 'url: '"//localhost/api/vs_1_0/availability"'' localhost will be replaced with another one. im sending the ajax from the localhost. – rzshss Jun 01 '15 at 10:24
  • you need to add Access-Control-Allow-Origin headers to your response script //localhost/api/vs_1_0/availability – RajeshK Jun 01 '15 at 10:58
  • it didn't worked for me. And also is this the correct way to send authentication details through header. because I'm catching the username & password from $_SERVER['PHP_AUTH_PW'] and $_SERVER['PHP_AUTH_USER'] variables. – rzshss Jun 03 '15 at 04:28
  • headers: {'Authorization': "Basic " + btoa("username:password")} this line is the thing that cause "Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource" error. – rzshss Jun 03 '15 at 04:54

0 Answers0