admin管理员组文章数量:1291251
I've been searching to the answer for a while, I want to be able to parametrise my plugins AJAX values. So e.g. rather than:
$.post('test.aspx', { name: 'bob' }, function(data){
....
});
I want to parametrise name and the value used in the post so e.g.
var var1 = 'name';
var var2 = 'bob';
$.post('test.aspx', { var1: var2 }, function(data){
....
});
My problem is that it inserts the value 'bob' but posts it as 'var1' rather than 'name'. And on the server side it is expecting name not var1.
Anyone got any ideas?
I've been searching to the answer for a while, I want to be able to parametrise my plugins AJAX values. So e.g. rather than:
$.post('test.aspx', { name: 'bob' }, function(data){
....
});
I want to parametrise name and the value used in the post so e.g.
var var1 = 'name';
var var2 = 'bob';
$.post('test.aspx', { var1: var2 }, function(data){
....
});
My problem is that it inserts the value 'bob' but posts it as 'var1' rather than 'name'. And on the server side it is expecting name not var1.
Anyone got any ideas?
Share Improve this question edited Aug 25, 2009 at 14:16 Ben Crouse 8,3485 gold badges37 silver badges51 bronze badges asked Aug 25, 2009 at 14:10 PhilPhil 4,0925 gold badges41 silver badges57 bronze badges 1- Why do you want to do this? Do you know ajaxSetup? docs.jquery./Ajax/jQuery.ajaxSetup#options – Daniel Moura Commented Aug 25, 2009 at 14:18
2 Answers
Reset to default 8Try building a hash for your data and then passing it to the post
function. Like so:
var data = {};
data[var1] = var2;
data[var3] = var4;
$.post('test.aspx', data, function(returnData) { blah, blah blah; });
This works for me:
$(document).ready(function()
{
var var1 = "bobsname";
var var2 = "bob";
ajax(var1, var2)
function ajax(name, variable)
{
$.ajax(
{
type: "POST",
data: name + "=" + variable,
url: "action.php",
success: function(html)
{
alert(html);
}
});
}
});
Im using the ajax function.
本文标签: javascriptjQuery ajax data post problemStack Overflow
版权声明:本文标题:javascript - jQuery ajax data post problem - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741509485a2382523.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论