I want to update WEB UI of Rails by ajax request triggered from java. However, it didn't work although I saw 200 OK message.
This is my step I did.
Make Ajax request in Java application (using HttpURLConnection object)
- X-Request-With : XMLHttpRequest
String url = "http://127.0.0.1:3000/java/fromjava"; String charset = "UTF-8"; String query = String.format("param1=%s¶m2=%s", param1, param2); try { HttpURLConnection urlConnection = (HttpURLConnection) new URL(url) .openConnection(); urlConnection.setDoOutput(true); urlConnection.setUseCaches(true); urlConnection.setRequestMethod("POST"); urlConnection.setRequestProperty("Accept-Encoding", "gzip,deflate,sdch"); urlConnection.setRequestProperty("Accept-Language", "ko-KR,ko;q=0.8,en-US;q=0.6,en;q=0.4"); urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); urlConnection.setRequestProperty("Accept", "*/*"); urlConnection.setRequestProperty("X-Requested-With", "XMLHttpRequest"); urlConnection.setRequestProperty("Referer", "http://192.168.43.79:3000/"); urlConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36"); urlConnection.connect(); OutputStreamWriter writer = null; writer = new OutputStreamWriter(urlConnection.getOutputStream(), charset); writer.write(query); writer.flush(); if (writer != null) try { writer.close(); } catch (IOException logOrIgnore) { }
respond to js in controller
- Set 'rack-CORS' to resolve same origin policy
class JavaController < ApplicationController skip_before_filter :verify_authenticity_token def fromjava respond_to do|format| format.js end end end
change WEB UI
[fromjava.js.erb] $('#container').empty().append('This is [ajax] text'); [java.html.erb] <div id="container"> This is default text </div>
Log message in log/development.log
Started POST "/java/fromjava" for 127.0.0.1 at [current date] Processing by JavaController#fromjava as */* Rendered java/fromjava.js.erb (0.0ms) Complete 200 OK in 43ms (View: 39.0ms | ActiveRecord: 0.0ms)
I saw the 200 OK message in log. However, Didn't update WEB UI. What is the problem?
Also, Are there other way to track the progress of the request & response ??