简体   繁体   中英

Ajax POST request not being processed

I'm using ajax to send a post request to c# code in the code behind my webpage in order to save content from tinyMCE editor to a file. The ajax function is called whenever the save button is pressed in the editor. I used very similar methods to process a post request in another webpage but for some reason it's not working on this one. The ajax code is:

 $.ajax({
            url: "Editor.aspx",
            data: { "contentCheckCode": tinyMCE.activeEditor.getContent() },
            type: "POST",
            success: function (response) {
                alert("Contents saved...");
            },
            error: function (xhr, status, errorThrown) {
                alert("Error: " + errorThrown);
            }
        });

And the C# code handling the request is:

     protected void Page_Load(object sender, EventArgs e)
    {
        //listens for POST request
        if (Request.HttpMethod == "POST")
        {
            BasePage sourcepage = (BasePage)Context.Handler;
            bool success;
            String requestContents;
            String path = Server.MapPath(sourcepage.Src);
            using (Stream inputStream = Request.InputStream)
            {
                using (StreamReader readStream = new StreamReader(inputStream))
                {
                    requestContents = readStream.ReadToEnd();
                }
            }
            if (requestContents.Contains("contentCheckCode"))
            {
                requestContents = editText(requestContents);
                success = saveToFile(requestContents, path);
                if (success)
                {
                    Response.Output.WriteLine("Contents saved");
                }
                else
                {
                    Response.Output.WriteLine("Error encountered");
                }
            }
        }
    }

When I debug through it seems as though the code is never even receiving the request, even though the ajax method is calling the success function every time, any suggestions as to why this might be? Again I've done this in almost exactly the same way on another webpage with no issue

EDIT: I added the following method to the C# code behind:

[System.Web.Services.WebMethod]
    public static void handlePOSTrequest(HttpContext request) {
        if (HttpContext.Current.Request.HttpMethod == "POST") {
            bool success;
            String requestContents;
            using (Stream inputStream = (HttpContext.Current.Request.InputStream))
            {
                using (StreamReader readStream = new StreamReader(inputStream))
                {
                    requestContents = readStream.ReadToEnd();
                }
            }
            if (requestContents.Contains("contentCheckCode"))
            {
                requestContents = editText(requestContents);
                success = saveToFile(requestContents, filePath);
            }
        }
    }

and placed a breakpoint beside it but the code never stops at that point implying it's not catching the request, am I missing something/doing something wrong here? Also just to check, in order to call the page method directly from the ajax call it's just a matter of changing the url to "Editor.aspx/handlePOSTrequest", right? Also the request is definitely being initiated

 $.ajax({
            url: "Editor.aspx/handlePOSTrequest",
            data: { "contentCheckCode": tinyMCE.activeEditor.getContent() },
            type: "POST",
            success: function (response) {
                alert("Contents saved...");
            },
            error: function (xhr, status, errorThrown) {
                alert("Error: " + errorThrown);
            }
        });

And your static method accepts an object of HttpContext when you're passing in "contentCheckCode"? I don't think the HttpContext class is serializable. And what does:

tinyMCE.activeEditor.getContent() represent? A string?

If so then your static function prototype should look like:

public static void handlePOSTrequest(string contentCheckCode) {

Try this and let me know how it goes.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM