Ignoreme
Ignoreme

Reputation: 183

Cross Domain post Javascript

So i'm trying to make a webpage, that launches local applications through a client.

The simplist solution I can think of is just posting or getting data to the port I want to use; say port 3000. Unfortuantely I've run into an error: XMLHttpRequest cannot load %3127.0.0.1:3000. Cross origin requests are only supported for HTTP.

My Question: Is there a way to post or get data to 127.0.0.1:3000 using XMLHttpRequest()?

sorry about the formatting mess, I played around with it for a long time and I couldn't get it to indent correctly D:

function Post_Command(Command_String){
if(typeof(Command_String) != "string"){
 console.log(
    "Invalid Call:\Post_Command(\n\t"+Command_String+":"+typeof(Command_String)+"\n)"
);
} else {
    xmlhttp=new XMLHttpRequest();
    xmlhttp.onreadystatechange=function(){
        if (xmlhttp.readyState==4 && xmlhttp.status==200){
            console.log(xmlhttp.responseText);
        }
    }
    xmlhttp.open(
        "POST",
        "127.0.0.1:3000",
        true
    );
    xmlhttp.send(Command_String);
}
}

Upvotes: 0

Views: 401

Answers (1)

Quentin
Quentin

Reputation: 943686

Yes, with provisos. Your first problem is that you have to use HTTP or HTTPS. You need a scheme on your URI (or you need to start it with // to maintain the current scheme, which is unwise if you are hitting a specific port).

"127.0.0.1:3000" --> "http://127.0.0.1:3000"

The server running on port 3000 needs to give permission to the site using CORS. The user must also be equipped with a browser that supports CORS.

Upvotes: 2

Related Questions