Reputation: 3756
so i have my original ajax call which works fine.
$.ajax({
type: "GET",
url: "index.php / blog / getbio",
data: { first_name: first_name, last_name: last_name button_type:button_type },
success: function (msg) {
$(button).text(msg);
button.slideDown("slow");
}
});
I'm trying to create a function so that I dont have to retype this every time and all i want to modify is the data and url fields of the ajax call. Though I don't know exactly how to replacte the data field outside of the ajax call. I've gotten as far as
var params = "first_name: first_name, last_name: last_name";
var url = "index.php / blog / getbio";
function ajax(url, params) {
$.ajax({
type: "GET",
url: url,
data: { params },
success: function (msg) {
$(button).text(msg);
button.slideDown("slow");
}
});
button.hide();
}
Though I know this doesn't work. SO my question is outside of the ajax function how can i have the variable params be properly created to nicely be inserted inside the ajax function.
Upvotes: 0
Views: 50
Reputation: 10068
function CreateFullName(f_name, l_name) {
params = {
"first_name": f_name,
"last_name": l_name
};
return params;
}
function ajax(url, first_name, last_name) {
$.ajax({
type: "GET",
url: url,
data: CreateFullName(first_name, last_name),
success: function (msg) {
$(button).text(msg);
button.slideDown("slow");
}
});
button.hide();
}
here the function is not bound with any hard coded value, use this generally with url and name values passed.
Upvotes: 0
Reputation: 45362
You can set data in json format for putting it into a string more easily:
var params = '{"first_name": "first_name", "last_name": "last_name"}';
var url = "index.php/blog/getbio";
$.ajax({
url: url,
data: params,
dataType:'json',
success: function (msg) {
$(button).text(msg);
button.slideDown("slow");
}
});
Upvotes: 1