Note that there are some explanatory texts on larger screens.

plurals
  1. POUnable to make CORS POST request in javascript to java web service(jersey)?
    primarykey
    data
    text
    <p>I am implementing Cross Resource Origin Sharing in Java Web services using Jersey.I created resource as followes:</p> <pre><code>@POST @Path("/getSubjects") @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) public Response getSubjects(TokenCheck tc) throws IOException, ServletException{ String token = tc.getToken(); String result = ""; if(!token.equals("") &amp;&amp; !token.equals(null)){ context.getRequestDispatcher("/GetSubjectsWs?token="+token).include(request, response); String subs = request.getAttribute("subjects").toString(); result = "{\"subjects\":\""+subs+"\"}"; }else { result = "{\"subjects\":\"['Invalid Token login again']\"}"; } JSONObject j = null; try { j = new JSONObject(result); } catch (JSONException e) { e.printStackTrace(); } return Response.status(200).entity(j).header("Access-Control-Allow-Origin", "*").header("Access-Control-Allow-Methods", "POST, GET, OPTIONS").header("Access-Control-Allow-Headers", "Content-Type:application/json").build(); } </code></pre> <p>and making post request using javascript as :</p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;meta charset="utf-8"&gt; &lt;title&gt;JavaScript Client&lt;/title&gt; &lt;script type="text/javascript"&gt; function restReq() { var url = "http://localhost:8888/WebservicesServer/restful/getserver/getSubjects"; var json = { "token":"8495C211F11C9B18E6651E03EB2995BC" }; var client = new XMLHttpRequest(); client.open("POST", url, true); client.setRequestHeader("Access-Control-Request-Methods", "POST"); client.setRequestHeader("Content-Type", "application/json"); client.send(json); client.onreadystatechange = function() { if (client.readyState == 4) { if ( client.status == 200) console.log("success: " + client.responseText); else console.log("error: " +client.status+" "+ client.responseText); } }; } &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;input type="button" value="getSubjects" onclick="restReq();"&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>When i clicked getSubjects Button in chrome I am getting <strong>error as : XMLHttpRequest cannot load ..localhost:8888/WebservicesServer/restful/getserver/getSubjects. Origin null is not allowed by Access-Control-Allow-Origin</strong>. <strong>But i am able to get response with GET request</strong>,problem is with POST request my browser url file:///E:/Documents%20and%20Settings/Srinivas/Desktop/wars/JSClient2.html (File system) I tried in many ways like by setting origin etc, still unable to get json response (Server is Tomcat 7) please help to overcome this problem.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload