0

I have written a function which basically takes some input as text and set of files. And using Generic handler i save the files and text all works fine. The issue is after the process i want to some some alert or success message how can i achieve that?

JS:

 var appID = $("#hidden_applicationID").val();
        var messageText = escape($("#screeningFeedback").val());

        var files = $("#fileScreening").get(0).files;
        var fileData = new FormData();

        for (var i = 0; i < files.length; i++) {
            fileData.append("fileInput", files[i]);
        }

        fileData.append("uniqueapplicationID", appID);
        fileData.append("messageText", messageText);

         $.ajax({
            url: "ScreeningUpload.ashx",
            data: fileData, type: "POST",
            processData: false,
            contentType: false,
            cache: false,
            dataType: "json",
             success: function (mydata) {
                 alert("hello");
                //$("#screeningFeedback").val();
                //showtoaster("Thank you for your submission!");

            }

        });

C#

 public void ProcessRequest(HttpContext context)
{ // DOING SOMETHING


    context.Response.Clear();
    context.Response.Write("<script type='text/javascript'>alert('Hello, world');</script>");

 }

How can i achieve the above tried many things but no luck.

Thank you

confusedMind
  • 2,573
  • 7
  • 33
  • 74

1 Answers1

0

If you are sending JavaScrip as response from backend then add that response in DOM. Browser will execute it.

$.ajax({
        url: "ScreeningUpload.ashx",
        data: fileData, type: "POST",
        processData: false,
        contentType: false,
        cache: false,
        dataType: "json",
        success: function (mydata) {

             $(document.body).append(mydata);
        },
        error: function(jqXHR, textStatus, errorThrown){
           alert("Status:"+textStatus+"\nError:"+errorThrown);
        }
    });
Tito
  • 663
  • 1
  • 8
  • 14
  • alert("hello"); this never runs so assuming this success: function (mydata) { never runs – confusedMind Apr 24 '20 at 14:52
  • If success method is not executed then there must be error. Confirm that ajax is pointed to right backend path. Backend is responding 200 status. Add an error handler in ajax for further debuging. – Tito Apr 24 '20 at 14:56
  • its a call to generic handler etc. not sure it works the same way – confusedMind Apr 24 '20 at 14:58
  • Doesn't matter whether url: "ScreeningUpload.ashx" is generic handler or route or physical path. Ajax doesn't know that. If there's no syntax error, an ajax call will endup in either success or fail(error). I've update the answer with error callback. – Tito Apr 24 '20 at 15:08
  • Generic handelr does not have a return type its void so not sure how ..? – confusedMind Apr 24 '20 at 15:09
  • Getting ERROR: Syntax Error:Unexpected end of Jason Input – confusedMind Apr 24 '20 at 15:13
  • 1
    Where did you get this? In error method? I guess, now you know your backend is getting wrong data. – Tito Apr 24 '20 at 15:17