2

I am trying to communicate 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.

Nahush Sarje
  • 137
  • 2
  • 3
  • 16

1 Answers1

1

That 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.com then you can not call http://YYYY.com 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();
jayesh
  • 2,422
  • 7
  • 44
  • 78
  • Thanks Jayesh. Do you have any example to do it from server side by using servlet as a technology for both applications? – Nahush Sarje Jul 29 '16 at 16:28
  • i added code for your reference this is basic java code using which you can call external http request but couple of other additional lib also provided http request with more parameters and configuration. – jayesh Jul 29 '16 at 17:05
  • Thanks Jayesh. can you tell me how to pass an string over here? like String content = "abcd123 Hjkl007* null"; as a post paramerter. – Nahush Sarje Aug 02 '16 at 18:12
  • can you post sepreat question for that.. @Nahush Sarje – jayesh Aug 03 '16 at 09:17
  • you can get some imformation from this http://stackoverflow.com/questions/4205980/java-sending-http-parameters-via-post-method-easily – jayesh Aug 03 '16 at 09:18
  • Hi Jayesh, thanks for help. I have posted new question related to this please comment on it. http://stackoverflow.com/questions/38740418/receiving-null-value-while-using-post-method-in-servlet/38749905#38749905 – Nahush Sarje Aug 04 '16 at 06:08