Hello, I am attempting to execute a webmethod using ajax from an aspx page. Essentially, I would like to redirect to another aspx page with a query string, but I need to do it through <a href>
because it is part of a jQuery menu.
Based on my understanding, I can only utilize ajax to invoke static webmethods, but I am unable to perform a redirect from within my static function.
Visual Studio highlights it with a red line indicating: "an object reference is required for the nonstatic field method or property System.Web.HttpResponse.Redirect(string)"
Below is the ajax call:
function redirect_to_profile() {
$.ajax({
type: "POST",
url: "personal_profile.aspx.cs.aspx/redirect_to_profile",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (res) {
alert("success");
},
error: function (res, msg, code) {
// log the error to the console
} //error
});
}
Here is the link:
<a onclick="redirect_to_profile()">Personal Profile</a>
Here is the webmethod inside personal_profile.aspx:
[WebMethod]
public static void redirect_to_profile()
{
dbservices db=new dbservices();
string user = HttpContext.Current.User.Identity.Name;
string id = db.return_id_by_user(user);
HttpResponse.Redirect("personal_profile.aspx?id="+id);
}