i would like to make an Ajax request like that:
function test(par1,par2) {
alert("add");
return(par1+par2);
}
$.ajax({
url: test,
dataType: "script",
data: 2,3,
success: function(data) {
alert(data);
}
});
I think that i am doing something wrong.
You need to call test
, not reference it:
url: test("/mypath/", "somelocation.php")
The difference is in the brackets, and provided arguments. Without brackets you pass a function reference without executing it. This does not resolve to the executed value. With brackets you execute the function and pass to the url
property the return value, which is what you want.
Of course, you need to decide what you pass to the function. In your case, you might have them in some variables.
Secondly, you need to change the value passed to data
as well. It should be an object where each property corresponds to a parameter name:
data: {par1: 2, par2: 3}
jQuery will do the translation to the url
format.
I have the impression your test
function was intended to add parameters to the url
, but since the data
property is intended for that, you probably can do away with that function. Just assign the basic url
to the url
property, without parameters:
url: "/mypath/mypage.php"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.