0

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.

  1. 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&param2=%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) {
            }
    
  2. 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
    
  3. change WEB UI

    [fromjava.js.erb]
    $('#container').empty().append('This is [ajax] text');
    
    [java.html.erb]
    <div id="container">
        This is default text
    </div>
    
  4. 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 ??

JY Choi
  • 53
  • 6
  • First you need to make sure that you are passing the the `.js` in your request url in order to get the response based on `fromjava.js.erb`. Second could you please show us the full content of your `fromjava.js.erb` – Moustafa Sallam Aug 09 '14 at 13:02
  • I checked that fromjava.js.erb received request in log file. Do I have to set request header to pass the request to .js file ?? – JY Choi Aug 09 '14 at 13:07
  • I updated this question. You can see full content of fromjava.js.erb. That's all. – JY Choi Aug 09 '14 at 13:13

1 Answers1

0

First you need to make sure that you are passing the the .js in your request url in order to get the response based on fromjava.js.erb. your url in the java code should be

String url = "http://127.0.0.1:3000/java/fromjava.js";

Second as I can see in the fromjava.js.erb you just passing a jQuery code, OK but you need to make sure that the jQuery code is being executed once the response is received on the client side, you just send the response which is code but it hasn't been executed on the client's machine.

Moustafa Sallam
  • 1,050
  • 1
  • 9
  • 17
  • How can I check whether jquery statement of fromjava.js.erb is executed ?? – JY Choi Aug 09 '14 at 13:15
  • I tried "String url = "http://127.0.0.1:3000/java/fromjava.js" but, It doesn't work althought I saw 200 OK. – JY Choi Aug 09 '14 at 13:18
  • I updated my answer with the correct url that should be passed. by the way your `java.html.erb` has no effect here as it is not part of your response – Moustafa Sallam Aug 09 '14 at 13:18
  • Your main issue is to make sure that the jQuery code is executed after the response is being received. – Moustafa Sallam Aug 09 '14 at 13:20
  • In the question, I saw the 200 OK and rendered java/fromjava.js.erb. Can I make sure the execution of jquery statement by this message ?? – JY Choi Aug 09 '14 at 13:23
  • where does the response go, normally if you initialize the ajax request from client's side the response goes to that client. but here the request initialized from Java app, the question where the response go? – Moustafa Sallam Aug 09 '14 at 13:29
  • OK your have specified the client url and port number in your request. just noticed it. OK the issue when the browser receives that response it doesn't execute it. I think you need to specify the expected type of the data when I response is received. – Moustafa Sallam Aug 09 '14 at 13:35
  • One thing I used to do with Javascript which I expect a js code I pass the reponse to `js eval function` so you need to pass your response to `javascript eval function` or whatever is equivalent in jQuery – Moustafa Sallam Aug 09 '14 at 13:38
  • Maybe respond to java because I didn't set the destination of response in Java. Can I set the destination of response in HTTP Reuqest Header??? I tried same url with your answer, However didn't work. Also, What is the mean of rendered "java/fromjava.js.erb" message. Thank you. – JY Choi Aug 09 '14 at 13:40
  • I don't know the way to pass to javascript eval function. Can you show me some example ?? – JY Choi Aug 09 '14 at 13:55
  • I know that the 200 OK message means that my intent(jquery statements) was executed in WEB, However I didn't see the result of my intent. Am I misunderstand the mean of 200 OK message ? If the log message is correct, is there another view that have same url ? and is it possible ? – JY Choi Aug 09 '14 at 14:04
  • 200 means that HTTP resquest was executed successfully on server. the status number indicates that the request was execute successfully and here is the the response if something went wrong on the server during request execution you will receive a response as well but with different status code based on the error occurred on the server. So the 200 has nothing to do with executing the jQuery code, it just means that your request was executed and response received with jQuery code but it doesn't mean that jQuery code has been executed. – Moustafa Sallam Aug 09 '14 at 14:45
  • Please read this ***[post](http://stackoverflow.com/questions/2793150/how-to-use-java-net-urlconnection-to-fire-and-handle-http-requests/2793153#2793153)*** it gives you a good idea on how to create a good http request and handle its response with Java – Moustafa Sallam Aug 09 '14 at 14:51
  • Thank you. I will check your link. Your answer was very helpful. – JY Choi Aug 09 '14 at 14:55