framework7 icon indicating copy to clipboard operation
framework7 copied to clipboard

app.request & XMLHttpRequest a mystery CORS problem, or a bug?

Open rapgithub opened this issue 8 years ago • 6 comments

I wonder if there is an issue with the use of app.request and CORS because my APP needs to request data from the remote server to serve the App all the CORS are OK and working perfectly if I send the POST request using XMLHttpRequest but when using app.request the reply from server it is all the time but curiously not always but with XMLHttpRequest it is constant OK :

server reply using app.request Ajax call:

 **_not allowed by Access-Control-Allow-Origin_**.

this works perfect all the time no problems with CORS.

var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
    if (xhr.readyState == 4 && xhr.status == 200) {
        var json = JSON.parse(xhr.responseText);
        console.log(json);
    }
};
xhr.open('POST', 'http://oauth.mydomain.com/auth/', true);
xhr.withCredentials = false;
xhr.setRequestHeader("Content-type", "application/json;charset=UTF-8");
xhr.onload = function() {
    if (xhr.status === 200) {
        console.log(xhr.responseText);
    } else {
        console.log('Request failed.  Returned status of ' + xhr.status);
    }
};
var data = JSON.stringify({
    "Token": "token_code_here"
});
xhr.send(data);

and now with app.request it works and suddenly stop working again :( no clues why but it happens only if I use app.request Ajax call... so maybe there is something here...

data = {
    Token: token_code_here
};
app.request({
    url: 'http://oauth.mydomain.com/auth/',
    method: "POST",
    dataType: "json",
    data: data,
    crossDomain: true,
    beforeSend: function(xhr) {
        xhr.withCredentials = false;
    },
    success: function(res) {
        console.log(res);
    },
    error: function(xhr) {
        console.log('error');
        console.log(xhr);
    }
});

again server reply using app.request Ajax call:

 **_Origin is not allowed by Access-Control-Allow-Origin._**.

Any clues how to fix this? CORS on server are fine defined but works well only when using XMLHttpRequest Ajax call...

:(

rapgithub avatar Sep 12 '18 17:09 rapgithub

I have been reading about some topics about Ajax and CORS and found out this:

https://zqzhang.github.io/blog/2016/04/18/why-use-onload-in-cross-domain-ajax.html

Maybe app.request works in the same way as onreadystatechange... I do not know if this is so but make sense why XMLHttpRequest works always instead of app.request...

any ideas?

rapgithub avatar Sep 12 '18 18:09 rapgithub

Request uses same XMLHttpRequest and it uses onload too. So would be good to see live example of where it is not working

nolimits4web avatar Sep 13 '18 09:09 nolimits4web

Try add header 'Access-Control-Allow-Origin: *' to your response.

vemaSolutions avatar Sep 13 '18 18:09 vemaSolutions

Yes the response has it all!!! and 'Access-Control-Allow-Origin: *' too... very weird this thing :( I will try to make a live example and post the link... here...

thanks for helping..

rapgithub avatar Sep 13 '18 20:09 rapgithub

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Dec 12 '18 20:12 stale[bot]

This issue has been automatically closed due to inactivity. If this issue is still actual, please, create the new one.

stale[bot] avatar Dec 26 '18 21:12 stale[bot]