JQuery ajax post post issue

I've been looking for an answer for a while, I want to be able to parameterize the values โ€‹โ€‹of my AJAX plugins. So, for example, rather than:

$.post('test.aspx', { name: 'bob' }, function(data){
....
});

      

I want to parameterize the name and value used in the message, for example

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" and not "name". And on the server side, it expects a name not var1.

Does anyone have any idea?

+2


source to share


2 answers


Try creating a hash for your data and then passing it to a function post

. For example:



var data = {};
data[var1] = var2;
data[var3] = var4;

$.post('test.aspx', data, function(returnData) { blah, blah blah; }); 

      

+8


source


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

0


source







All Articles