HTTP POST failed

Ask, and ye shall receive.
johnzhel
Posts: 3
Joined: 16:48, 07 Jan 2015

HTTP POST failed

Postby johnzhel » 07:12, 10 Mar 2015

I am trying to post some data from mobile app to a remove server. I found the following JS code and tried it in the evothings JavaScript Workbench, it worked well. But when I pushed the same code to mobile app with some HTML (document.getElementById("response").innerHTML = , to display responseText coming back), I never received http.status == 200, the status is always 0, never received any resonseText back, either. Why is that?

Thanks a lot for your time and help! -John

Code: Select all

var http = new XMLHttpRequest();
var url = "http://24.218.156.245:8080/DataIntegrator/JsonHandler";
var params = "lorem=ipsum&name=binny";
http.open("POST", url, true);

//Send the proper header information along with the request
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", params.length);
http.setRequestHeader("Connection", "close");

http.onreadystatechange = function() {//Call a function when the state changes.
    if(http.readyState == 4 && http.status == 200) {
        alert(http.responseText);
    }
}
http.send(params);

Fredrik
Site Admin
Posts: 196
Joined: 15:00, 18 Nov 2013

Re: HTTP POST failed

Postby Fredrik » 10:41, 10 Mar 2015

XMLHttpRequest is limited by the CORS system. Status 0 is what you get when the browser blocks the request. This is very badly documented.

If you can get the remote server to send the HTTP header "Access-Control-Allow-Origin: *", it may start working again.

johnzhel
Posts: 3
Joined: 16:48, 07 Jan 2015

Re: HTTP POST failed

Postby johnzhel » 13:29, 10 Mar 2015

@Fredrik Thank you for your help. I have a few follow up questions.

1. What is the difference between the evothings app and the evothings JS workbench? Why one doesn't work (0) and the other one works (200)?

2. Is there any other way to do http POST in evothings app besides XMLHttpRequest?

Thanks,
-John

Fredrik
Site Admin
Posts: 196
Joined: 15:00, 18 Nov 2013

Re: HTTP POST failed

Postby Fredrik » 13:52, 10 Mar 2015

1. As far as I know, there should be no practical difference. I know that ordinary Cordova apps are able to bypass CORS when the script that generates the request is loaded from the file:// system. Evothings Client can't do that because it loads apps from the Workbench, and so has a source domain on the form "http://192.168.x.x". Even the "Connected" screen is loaded from the Workbench in this manner.

Maybe you pressed "node eval" instead of "eval selection", thus running your XmlHttpRequest in the node.js context.

2. No. We've been looking for / tinkering with HTTP plugins that would be able to do this, but we haven't yet got anything stable enough to include in the Client. Also not a lot of people ask for it.

Can you get the server to send the Access-Control-Allow-Origin header?


Return to “Questions and answers”

Who is online

Users browsing this forum: No registered users and 1 guest