在服务器端脚本中的ajax调用中访问jquery数据变量 [英] Accessing jquery data variable in ajax call in server side script

查看:52
本文介绍了在服务器端脚本中的ajax调用中访问jquery数据变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中有几个ajax调用,它们都转到同一个文件服务器端.我想知道是否可以使用变量...我想有一种方法可以将调用与其他调用区分开,当然,可以使用变量选择文件中的哪个脚本发送回客户端.在脚本以任何方式运行之后,我不需要使用变量.还是我走错路了?

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?

例如,这是我的ajax调用之一

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 (尝试过)

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

无济于事.

推荐答案

Aj就在这里,因为我没有列出我正在使用的服务器端代码,所以我想使用php,但简单地说,我将ajax调用更改为

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'},
});

其中键是currentuser1var,值variable1是字符串,并且省略了定义变量的位置,否则位于代码中.这样,URL可以正确地到达/users/show?currentuser1var=variable1服务器.然后在我的目标文件中添加我的ruby代码以使用这些变量.

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.

这篇关于在服务器端脚本中的ajax调用中访问jquery数据变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆