Ajax returns masterpage html instead of returns from my method.
Here is my client code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Page1.aspx.cs" Inherits="Page1"
MasterPageFile="~/MasterPage.master" EnableEventValidation="false" %>
function saveForm() {
let formData = new FormData();
formData.append("name", "john");
$.ajax({
type: "POST",
url: 'Page1.aspx?method=SaveForm',
data: formData,
contentType: false,
processData: false,
cache: false,
success: onSuccessSaveForm,
failure: function (response) {
alert(response.d);
},
error: function (response) {
alert(response.d);
}
});
}
Here is my server code:
public partial class Page1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["method"] != string.Empty && Request.QueryString["method"] != null)
{
Type thisType = this.GetType();
MethodInfo theMethod = thisType.GetMethod(Request.QueryString["method"]);
theMethod.Invoke(this, null);
}
}
}
public string SaveForm()
{
Result result = new Result();
...code
return JsonConvert.SerializeObject(result); -->(*expect to return this)
}
Expectations:
stop at theMethod.Invoke(this, null)
and returns to my ajax call
Result:
it continues to page_load
method at MasterPage.master.cs, and returns the whole html as result
How to pass result from theMethod.Invoke(this, null)
to front end. And if possible, not having to go MasterPage.master.cs too and not involve refreshing the page, like using [System.Web.Services.WebMethod]
.
2
Answers
Orait. So basically, all i have to do is write the result to Response and then end it. So the page_load method will be:
I suspect some error is occurring here.
Note that you do need to "clear" the response, else you will receive the whole page back.
Here is some sample working code:
And code behind:
So, above does work, but I suggest it better to dispense with all that code in the page load event, and simply call a web method directly.
So, say we wish to grab first and last name from 2 textboxes, and pass to server using a web method (end point).
The server web method will combine into full name, and return this value.
Markup:
And now code behind:
So, note now we don’t have to parse out the paramters, and we don’t have to parse out the page method and we don’t have to use reflection to trigger a method of our choice.
The result is thus this:
Note close that any value(s) returned are found in the ".d" property of the ajax response, and this is just a .net quirk for reasons of security.
So, as you can see, we vast can reduce parsing, and you can tag a code behind routine as a WebMethod. Such methods must be static, and thus that method can’t see or grab control values in the code behind, but you are free to pass such values from the client side ajax call.
All in all, I don’t see the advantages of bothering with a JavaScript Form object, a .ajax post of that form data, and then using URL Query string + reflection.
And yes, in most cases, using the first example, the whole page is returned, but you can clear the response, and then using Response.Write to control what is returned to the .ajax post.
The page should not go blank, nor appear to experience a post back in both of the above examples.
I not tested using a JavaScript Form object and post with a master page.
However, I often use a web method with master pages, and the web methods can freely be placed in any web page, but not the master page.
Better would be to create a .asmx web end point (web service), and the above ajax code (non form post) example will work much the same.
If such code is to be called and used in the master page, and not the child page?
Then I much suggest you use a .asmx (create a web service).
eg:
Hence, call and use the WebMethod end points in that .asmx page.