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?
 
hieloConnect With a Mentor Commented:

>>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
 
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
 
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
 
hieloConnect With a Mentor Commented:
>>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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.