0

I have to make a request from javascript that is on server A, to a php file that is on server B. I can access both servers.

But something is getting wrong. I always get ready state 0, status 0.

This is the latest thing I tried: Please advice what I'm doing wrong. thanks.

Server A:

$.ajax({
    type: "GET",
    url: 'http://server_B/request.php',
    data: form_data,
    dataType: 'json',
    success: function (resp) {
        alert("Successful");
        console.log("Response completed");
        console.log("resp is" + resp);
    },
    error: function (xhr, error) {
        console.log("readyState: " + xhr.readyState + "\nstatus: " + xhr.status);
        console.log("responseText: " + xhr.responseText);
        alert("Error occurred.");
    }
});

Server B: request.php

<?php
    header('Content-Type: application/json');
    header('Access-Control-Allow-Origin: '.$_SERVER['HTTP_ORIGIN']);
    header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
    header('Access-Control-Max-Age: 1000');
    header('Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With');

    # do the work and save it on $result array.
    print json_encode($result,true);
?>
8
  • I'm not familiar with jsnop, and I don't get any result on console so to understand what is happening.. Commented May 8, 2014 at 16:55
  • in your ajax call, use dataType: 'jsonp' - learn about JSONP Commented May 8, 2014 at 16:56
  • Do you get a SecurityError from the XMLHttpRequest logged in your console? Does it work if you allow *? Commented May 8, 2014 at 16:58
  • What are you seeing in the console? What specific request is being sent? Is it an OPTIONS, or a GET? Commented May 8, 2014 at 17:05
  • You should have an error in your javascript console. In Chrome (and maybe others, but Chrome for sure), it will tell you exactly what the problem was. Commented May 8, 2014 at 17:08

1 Answer 1

-1

To use ajax between two different domains you will have to use jsonp

You can find more help in here

Sign up to request clarification or add additional context in comments.

2 Comments

This would be better suited as a comment. But, to each his own
"you will have to use jsonp" - No, you can also use CORS, which is what the OP is attempting to do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.