I am trying to municate with an enterprise application from an web application mainly via javascript using ajax. I tried a lot to solve this issue but not succeeded. I saw several online httppost tool there I am able to see the response text but it is not happening from my end. Each time I am receiving an message like "Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://url. (Reason: CORS header 'Access-Control-Allow-Origin' missing)."
My code:
var url = "use_url";
var method = "POST";
var regid = "null";
var UNAME = "abcd089";
var PASSWORD = "abcd007*";
var forLogin = "10 112 " +UNAME+ " " + PASSWORD + " " + regid + " 01";
var async = true;
var request = new XMLHttpRequest();
request.open('POST', url, async);
request.onload = function(){
//HTTP response
//if(request.readyState === 4 && request.status === 200){
var status = request.status;
var statusData = request.responseText;
console.log(status);
console.log(statusData);
console.log(request);
//}
};
request.setRequestHeader("X-Requested-With", "XMLHttpRequest");
request.setRequestHeader("Content-Type", "text/plain;charset=UTF-8");
request.setRequestHeader("Cache-Control", "no-cache");
request.send(forLogin);
I am looking for a solution to get response text. I saw online some solutions but they all are talking about setting response header, but some online httppost sites are working fine on data and producing response text. I am looking solution in javascript.
I am trying to municate with an enterprise application from an web application mainly via javascript using ajax. I tried a lot to solve this issue but not succeeded. I saw several online httppost tool there I am able to see the response text but it is not happening from my end. Each time I am receiving an message like "Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://url. (Reason: CORS header 'Access-Control-Allow-Origin' missing)."
My code:
var url = "use_url";
var method = "POST";
var regid = "null";
var UNAME = "abcd089";
var PASSWORD = "abcd007*";
var forLogin = "10 112 " +UNAME+ " " + PASSWORD + " " + regid + " 01";
var async = true;
var request = new XMLHttpRequest();
request.open('POST', url, async);
request.onload = function(){
//HTTP response
//if(request.readyState === 4 && request.status === 200){
var status = request.status;
var statusData = request.responseText;
console.log(status);
console.log(statusData);
console.log(request);
//}
};
request.setRequestHeader("X-Requested-With", "XMLHttpRequest");
request.setRequestHeader("Content-Type", "text/plain;charset=UTF-8");
request.setRequestHeader("Cache-Control", "no-cache");
request.send(forLogin);
I am looking for a solution to get response text. I saw online some solutions but they all are talking about setting response header, but some online httppost sites are working fine on data and producing response text. I am looking solution in javascript.
Share Improve this question asked Jul 29, 2016 at 16:07 Nahush SarjeNahush Sarje 1372 gold badges3 silver badges16 bronze badges1 Answer
Reset to default 1That is not allowed from javascript side if you are on different domain then you need to do at from server side.
browser has cross-origin blocked you can not do any request to non host domain from javascript using ajax. if you are on http://XXXX. then you can not call http://YYYY. from javascript for post request
if you have full control over both domain that you can change your server config to allowed that domain to access resource but that is not preferable as security..
below code you can use to do http post request from server
URL url = new URL("your url");
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
httpCon.setDoOutput(true);
httpCon.setRequestMethod("POST");
OutputStreamWriter out = new OutputStreamWriter(
httpCon.getOutputStream());
System.out.println(httpCon.getResponseCode());
System.out.println(httpCon.getResponseMessage());
out.close();