sscotti
asked on
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 :param2val ue, ....}, 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,ar g2) { //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.
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
function jQueryPostAJAX(url,arg1,ar
$.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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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:pas sword . . .
or generally: where args = "param1Name:param1Value,pa ram2Name:p aram2Value . . ."
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,ar g2) { //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.
Someting like this:
userid:userid,password:pas
or generally: where args = "param1Name:param1Value,pa
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,ar
$.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.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Just post the form with the URL something like this:
xyz.html?p=io&q=fp
Regards.