jQuery AJAX post and parameter list

Is there a way to pass a URL and a  parameter list to the jQuery AJAX post function?

Basically want to do something like this:

function jQueryPostAJAX(url,args) {
                  $.post(url, args,
                           function(data){
                                -------------
                                -------------
                           });
}

where I can pass in the URL and an argument list of the form {param1:param1value,param2:param2value, ....}, where the param names, values and number varies depending upon the calling function.  I tried just passing in a javascript string of the form and it doesn't seem to be working.  I know that you can pass in values for parameters like:

function jQueryPostAJAX(url,arg1,arg2) {  //or even an array I think
                  $.post(url, {arg1 name: arg1,arg2 name,arg2}
                           function(data){
                                -------------
                                -------------
                           });
}

but not sure that you can pass in an entire string or if you can parse the string into name value pairs and build a parameter list dynamically in the function.  Mostly want to have a more generalized function rather than building a separate function for different parameter lists.


LVL 5
sscottiAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

StealthyDevCommented:
Yes, you can post with query sting.

Just post the form with the URL something like this:

xyz.html?p=io&q=fp

Regards.
0
hieloCommented:

>>Basically want to do something like this:

function jQueryPostAJAX(url,args) {
                  $.post(url, args,
                           function(data){
                                -------------
                                -------------
                           });
}

That should work, BUT you need to encode the args. To clarify, if you are sending:
discount=10%&id=3&name=John O'Malley

That "%" symbol (among others) would need encoding. To be safe, you are better off encoding all the values:

var p="";
p+="discount=" + encodeURIComponent("10%") ;
p+="id=" + encodeURIComponent("10%") ;
p+="name=" + encodeURIComponent("John O'Malley") ;


THEN call your function:
jQueryPostAJAX('yourpage.php',p);

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sscottiAuthor Commented:
I was actually looking to pass in a string or array where I pass in the param name and the value for the param:

Someting like this:

userid:userid,password:password . . .

or generally:  where args = "param1Name:param1Value,param2Name:param2Value . . ."

I can parse that with javascript like temp=args.split(","), iterate through that array:
str = url +"?";
for (x=0; x<temp.length.x++) {
str+= temp[x].split(":")[0]+"=";
str+=temp[x].split(":")[1]+"&";
}
There is probably a better way, like just passing in the string already constructed or passing in an array?

However, the issue that I seem to be having is the the jQuery post isn't working.  Looks like it is sending a get or doing someing else.   The Jquery docs do say that the general format is:

function jQueryPostAJAX(url,arg1,arg2) {  //or even an array I think
                  $.post(url, {arg1 name: arg1,arg2 name,arg2},
                           function(data){
                                -------------
                                -------------
                           });
}
 where arg1 name, arg2 name are not variables but simply parameter names, although the values can be variables passed in to a function.

see"  http://api.jquery.com/jQuery.post/

Here is another example from http://articles.sitepoint.com/article/ajax-jquery/3 similar to what I want to do:

 $.post("backend.php",{  
       message: $("#msg").val(),  
       name: $("#author").val(),  
       action: "postmsg",  
       time: timestamp  
     }, function(xml) {  
   addMessages(xml);  
 });

except I want the "message", "name","action", "time" to be passed in along with the values.  From what I understand with jQuery, the parameter names are static.  I am actually wanted to submit a form via AJAX, but I'd like to pass in the parameters and URL rather than having a customized function.

There is a more general form which might be best for my needs:

$.ajax({
   type: "POST",
   url: "some.php",
   data: "name=John&location=Boston",
   success: function(msg){
     alert( "Data Saved: " + msg );
   }
 });  

In that form, can the url be the entire query string and it will still post the to the page.
0
hieloCommented:
>>I can parse that with javascript like temp=args.split(","), iterate through that array:
Yes
function jQueryPostAJAX(url,args) {
	args=args.replace(/[&]/g,'%26').replace(/[=]/g,'%3D').replace(/,/g,'&').replace(/[:]/g,'=');
                  $.post(url, args,
                           function(data){
                                -------------
                                -------------
                           });
}

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.