As if there were aaa.com and bbb.com
Write some code in javascript on aaa.com and access bbb.com
When there is no arbitrary page (html) in bbb.com, 404 etc.
Is it possible to receive a response code?

How should I write it if possible?
I would appreciate it if you could tell me.


Aaaaa.com and bbb.com are both under their control.
You can get code 200 below, but a page with no code (404 not found) will generate a cross-origin error.

Add the following to bht.com's .htaccess

header append Access-Control-Allow-Origin: *

Run the following javaScript on aaa.com

var root_path = location.pathname;
var OTHER_SITE_DOMAIN = "https://bbb.com";
var otherSiteSameContentUrl = OTHER_SITE_DOMAIN + root_path;
console.log (otherSiteSameContentUrl);
    var xhr = new XMLHttpRequest ();
    console.log ('UNSENT', xhr.status);
    xhr.open ('GET', otherSiteSameContentUrl, true);
    console.log ('OPENED', xhr.status);
    xhr.onprogress = function () {
        console.log ('LOADING', xhr.status);
    xhr.onload = function () {
        console.log ('DONE', xhr.status);
        $('body'). append ('DONE:' + xhr.status);
    xhr.send (null);
    / **
     * Outputs the following:
     * UNSENT 0
     * OPENED 0
     * LOADING 200
     * DONE 200

How can I get 404 in [xhr.status] when accessing a page that doesn't exist?

  • Answer # 1

    Generally, you cannotdo it with only JavaScript in the browser.

    You can't access the data fetched from other servers from JavaScript, except for "SENDER approved by CORS" and "JSONP".

  • Answer # 2

    Although it seems that discussions are already in progress, just in case.


    I want to get [this.status].

    Please check if you can get it below.


  • Answer # 3

    By making an original 404 and redirecting to it, we were able to get a 404 response code safely!

Related articles