1

I have a few ajax calls in my app they all go to the same file server side. i want to know if I can use a variable... I want to have a way to distinguish the call from other calls and of course, use the variable to select which script in the file to send back to the client. I do not need to use the variable after the script has run in any way. Or am i going about this the wrong way?

for example this is one of my ajax calls

var variable1 = 'currentuser1var';

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

then the script in my server side file would be

if (currentuser1var) { script here }

else if (currentuser2var) { script here }

...

I am not sure how to access the string, inside the object call. Do i need to access the object first then the string? Or just reference the variable some how.

EDIT Tried

if(typeof(currentuser1var) != "undefined") { script here }

to no avail.

Lee Eather
  • 345
  • 3
  • 16
  • what is your server side language? – Rick Mar 17 '17 at 07:10
  • if it is php, you can view variable $_GET or $_REQUEST – BQ Khánh Mar 17 '17 at 07:12
  • See @Aj334's answer. It might be more clear for you if you use the url in the call like this: `url: '/users/index?currentuser1var='+variable1,`. – sandrooco Mar 17 '17 at 07:27
  • Im using ruby on rails in a js.erb file. I searched and found isset for php and tried to find the js version of this such as this post [here](http://stackoverflow.com/questions/4231789/is-there-something-like-isset-of-php-in-javascript-jquery) It doesnt show any errors and the server renders the posts but they do not show up on the page. i will append what I have tried. – Lee Eather Mar 17 '17 at 08:42

2 Answers2

1

If you were using PHP, your server side script would be:

if(isset($_GET['currentuser1var'])) {


    ... script to process the currentuser1var variable ...


} else if (isset($_GET['currentuser2var'])) {


    ... script to process the currentuser2var variable ...


}
Ashil John
  • 7,362
  • 4
  • 19
  • 34
  • yes i found this in another post, but i am using ruby on rails in a js.erb file server side. see my appended code. The script will not read past the first script. ie the first `if` statement fires but anything after doesnt make the page. however i will say the server shows no errors. Its hard to debug when you cant see where the errors are. Thanks for steering me in the right direction though :) – Lee Eather Mar 17 '17 at 08:50
  • Here's another [reference to isset() equivalent of JS](http://stackoverflow.com/questions/2281633/javascript-isset-equivalent). And another on [Detecting an undefined object property ](http://stackoverflow.com/questions/27509/detecting-an-undefined-object-property) . – Ashil John Mar 17 '17 at 09:17
  • ok thanks. Perhaps I will try and break down the problem with a alert for my response and keep things simple and see where the problem lays. Thanks for your help. I will add a answer if I get it working. – Lee Eather Mar 17 '17 at 09:25
  • Yeah, but I've always found using console.log(1) -> console.log(n) much better than alert() as alerts tend to drive you crazy after a while. ':D. So, perhaps you could just view your console and see which number the logging stops at and then focus on that particular section for the problem. – Ashil John Mar 17 '17 at 09:43
  • so a number for each statement and see where the logging stops? – Lee Eather Mar 17 '17 at 10:05
  • Yep, you could start with a number above each code block (if you've got a lot of code in the page). And once you've figured out which the last working code block is, you could go for the kill with a number above each statement after that block to pin-point the problem. – Ashil John Mar 17 '17 at 10:14
  • nice. Ill report back. – Lee Eather Mar 17 '17 at 10:47
0

Aj was right here I guess for php as I did not list what server side code I was using, but simply put, I changed my ajax call to

return $.ajax({
  type: 'GET',
  url: '/users/show', 
  data: { currentuser1var: 'variable1'},
});

where the key is currentuser1var and the value variable1 is a string and leave out defining the variable else where in the code. That way the url comes through correctly to the server being /users/show?currentuser1var=variable1. And in my destination file add my ruby code there to use the variables.

Lee Eather
  • 345
  • 3
  • 16