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.

4
  • what is your server side language? Commented Mar 17, 2017 at 7:10
  • if it is php, you can view variable $_GET or $_REQUEST Commented Mar 17, 2017 at 7: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,. Commented Mar 17, 2017 at 7: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 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. Commented Mar 17, 2017 at 8:42

2 Answers 2

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 ...


}
Sign up to request clarification or add additional context in comments.

7 Comments

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 :)
Here's another reference to isset() equivalent of JS. And another on [Detecting an undefined object property ](stackoverflow.com/questions/27509/…) .
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.
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.
so a number for each statement and see where the logging stops?
|
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.