AJAX request failed with unknown reason (jQuery)

I have a service that returns JSON data: http://api.drag2droid.shamanland.com/captcha?base64

I am trying to execute a simple AJAX request:

$.ajax({
    type: "get",
    url: "http://api.drag2droid.shamanland.com/captcha?base64",
    dataType: "json",
    success: function(data) {
        $("body").html(data);
    },
    error: function(jqXHR, textStatus, errorThrown) {
        $("body").html("ajax failed: " + textStatus + ", " + jqXHR.status + " " + errorThrown);
    }
});

Result:

ajax failed: error, 0 

But if I just paste this url into the address bar in my browser, I can see the json response.

Does anyone know of possible traps?

JSFiddle: http://jsfiddle.net/shomeser/n5TjL/

Edition:

In fact, I already configured my server side to allow all requests from any domain with any header, PHP code:

if ($_SERVER["REQUEST_METHOD"] == "OPTIONS") {
    header("Access-Control-Allow-Origin: *");
    header("Access-Control-Allow-Credentials: true");
    header("Access-Control-Max-Age: 86400");
    header("Access-Control-Allow-Methods: GET, POST, PUT, OPTIONS");

    if (isset($_SERVER["HTTP_ACCESS_CONTROL_REQUEST_HEADERS"])) {
        header("Access-Control-Allow-Headers: {$_SERVER["HTTP_ACCESS_CONTROL_REQUEST_HEADERS"]}");
    }

    exit(0);
}

Edition:

On the Network tab of the Firebug plugin, I see that no content was found: enter image description here

But the direct GET-reqeust from the browser shows the full content.

0
source share
4

, , . , PHP-from , - if METHOD == OPTIONS. .

Access-Control-Allow-Origin OPTIONS.

:

if (isset($_SERVER["HTTP_ORIGIN"])) {
    header("Access-Control-Allow-Origin: {$_SERVER["HTTP_ORIGIN"]}");
}

if ($_SERVER["REQUEST_METHOD"] == "OPTIONS") {
    if (isset($_SERVER["HTTP_ACCESS_CONTROL_REQUEST_METHOD"])) {
        header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
    }

    if (isset($_SERVER["HTTP_ACCESS_CONTROL_REQUEST_HEADERS"])) {
        header("Access-Control-Allow-Headers: {$_SERVER["HTTP_ACCESS_CONTROL_REQUEST_HEADERS"]}");
    }

    exit(0);
}

@Patrick Evans , =)

+1

"json", json-. , .

URL: JQuery API

"json": JSON JavaScript. JSON ; JSON . jQuery 1.9, ; null {}. (. Json.org JSON.)

"jsonp": JSON JSONP. "? Callback =?" URL-, . "_ = [TIMESTAMP]" URL-, true.

0

The reason is that CORS (that is, a cross-domain problem). To abort jsonp instead of json.

dataType: "jsonp",
-1
source

Source: https://habr.com/ru/post/1686551/


All Articles