0

I have a ajax call client side (code listed below) which adds some data parameters then sends that url to the server to get some code in a js file. I just wanted to see what window.location.search returned by adding in a alert in that js file to send back but the alert is empty, by that i mean, the query string returned is blank, the same as console.log(window.location.search). I can get window.location to work, returns http://localhost:3000/ but adding the search part which gets the query string value returns empty from the server.

ajax call

var variable1 = 'currentuser1var';
$.ajax({
      type: 'GET',
      url: '/users/index',
      data: {currentuser1var: variable1},
      dataType: 'script',
    });

Server response

Started GET "/users/index?currentuser1var=currentuser1var&_=1490258560962" for ::1 at 2017-03-23 19:42:42 +1100
Processing by UsersController#index as JS
  Parameters: {"currentuser1var"=>"currentuser1var", "_"=>"1490258560962"}
  Rendering users/index.js.erb
  Rendered users/index.js.erb (0.0ms)
Completed 200 OK in 312ms (Views: 172.1ms | ActiveRecord: 0.0ms)

I am just wondering if anyone might know why this is, if there is a current issue, or problem with what I am trying to do, or something you may know of from doing this?

Lee Eather
  • 345
  • 3
  • 16
  • The JS code will still be executed on the client, even when fetched from the server, so the result will be exactly the same. For that reason this seems like an XY question. What problem are you actually trying to solve? – Rory McCrossan Mar 23 '17 at 09:35
  • I wanted to use the variables to distinguish between different code blocks from other ajax calls in the same js file server side as I have a few different ajax calls but no way to distinguish which code to get when the ajax call is made. – Lee Eather Mar 23 '17 at 09:52
  • 1
    In which case you need to execute the logic on the server, not retrieve the JS files and run them on the client – Rory McCrossan Mar 23 '17 at 09:53
  • Makes plenty of sense, I thought putting that code in the file would retrieve the query string after the request is sent. Could you give me a idea of what I may be able to do? Please. – Lee Eather Mar 23 '17 at 09:57
  • Yeah, unfortunately not as it only retrieves the text of the file then runs it on the client - it's the same as typing it in to your page and executing it there. Unfortunately I can't really offer any more help as I don't know which server side languages you have access to. I'd suggest starting a new question about this, stating clearly what the issue is you need to solve. – Rory McCrossan Mar 23 '17 at 09:59
  • Thanks. yes I realize this may be another question. Thanks for your help. :) – Lee Eather Mar 23 '17 at 10:04
  • I managed to get things sorted out after Rorys help with what I did shown with correct answer [here](http://stackoverflow.com/questions/42851221/accessing-jquery-data-variable-in-ajax-call-in-server-side-script) – Lee Eather Mar 24 '17 at 07:22

0 Answers0