如何在ajax调用中使用键/值对的jquery数据选项 [英] how to use key/value pairs in ajax call for jquery data option

查看:102
本文介绍了如何在ajax调用中使用键/值对的jquery数据选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究如何向ajax请求中添加变量,这些变量可以在服务器端脚本中使用.我在此处中检查了此stackoverflow帖子,并检查了jquery api docs 此处用于ajax请求.我的代码未定义错误variable.

Ive been checking out how to add variables to a ajax request which I can use in my server side script. I checked this stackoverflow post here and checked the jquery api docs here for a ajax request. I am getting error variable in my code is not defined.

我的代码中有这一行

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

我希望它对结果执行类似的操作,以便将所有不同的脚本保存在一个文件中.

I was wanting it to do something like this with the results so I can keep all my different script in the one file.

if ($.get("currentusershow")) { 
// do something here 
}
else if...

我不确定如何在我的代码中添加value?

i am not sure how to add the value to my code?

我的网址也无法访问保存我的代码的show.js.erb.

Also my url does not work going to the show.js.erb where my code is kept.

推荐答案

马蒙(Mamun)很合适,因为我在问题中没有很好地解释自己,但我认为我会发布此内容并用我正在尝试的方式澄清我的问题去做. Ajax调用应该是

Mamun was kind of right here as I did not explain myself very well in my question but I thought I would post this and clarify my question with what I was trying to do. The ajax call should be

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

其中键是currentusershow,值variable是字符串,并省略了定义变量的其他代码.这样,URL可以正确地到达/users/show?currentusershow=variable服务器.然后在我的目标文件中添加我的ruby代码以使用这些变量.在我的问题中,该代码更多是php类型的代码,因为我当时不知道自己在做什么.

where the key is currentusershow and the value variable 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?currentusershow=variable. And in my destination file add my ruby code there to use the variables. In my question that code was more a php type code as I did not know what I was doing at the time.

这篇关于如何在ajax调用中使用键/值对的jquery数据选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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