1

I have a Asp Button as follow:

<asp:Button ID="btnSubmit" runat="server"  OnClientClick=" if (!ValidateForm()) return false;" PostBackUrl="https://some/website/dot/com"  Class="btn_submit"/>

In my JS, in the middle of ValidateForm() function I need to call a ASHX handler and do some stuff there.

I am using this to call the ASHX file:

$.get("../MasterPages/AHMHandler.ashx?test=1", function (r) {
            alert(r);
        });

But it is not working.

Also I have tried $.ajax as follow and it always alerts error message. How can I see where is the source of error?

$.ajax({
        type: "GET",
        url: "../MasterPages/AHMHandler.ashx?t=1",
        dataType: "HTML",
        success: function (msg) {
            alert(msg);
        },
        error: function () {
            alert("Error");
        }

    });

Any idea???????

Update:

Another weird behavior. The get function is not working in following format:

alert("Before Get call");

        $.get("../MasterPages/AHMHandler.ashx?t=1", function (r) {
            alert(r);
        });

But when I add another alert after the get method, it work and triggers the breakpoint!!!

alert("Before Get call");

        $.get("../MasterPages/AHMHandler.ashx?t=1", function (r) {
            alert(r);
        });

        alert("after Get call");

Any suggestions?

amir moradifard
  • 353
  • 1
  • 8
  • 26
  • 2
    First, that won't work. Ajax calls are async, so the `ValidateForm` function won't block until completion. Second, you can learn to debug javascript [here](http://www.netmagazine.com/tutorials/javascript-debugging-beginners). Third, if you need to validate on the server-side (which you definitely should), why not just submit the form and validate on the server instead of doing an ajax call to validate? – Jason P Aug 05 '13 at 18:13
  • Thanks Jason for your reply. I never said I am using Ajax to validate. Validate is a function to evaluate some stuff in page and this should be done in client side (Javascript) and in the last, a server-side code is called (not validation). This part is done by Ajax. Thanks for 1st and 2nd points. – amir moradifard Aug 05 '13 at 18:17
  • What do you mean by not working? 404, 500, undefined, finished too late? Press F12 and look at the console tab (in most browsers). – Logan Murphy Aug 05 '13 at 18:19
  • Logan, I put breakpoint in my ashx backend code but it never triggers. – amir moradifard Aug 05 '13 at 18:40
  • Logan and Jason, please check the update! – amir moradifard Aug 05 '13 at 18:45
  • Where are you making the ajax call? Is it in a function triggered by `onClientClick`? I'm wondering if the click is causing a postback and the ajax call isn't actually getting sent. – Jason P Aug 05 '13 at 20:13
  • Jason, So how-come it works when I put an alert after get function??? – amir moradifard Aug 06 '13 at 05:01
  • @amirmoradifard If you want me to be notified of your response, put the `@` in front of my name. I think it works when you put the alert, because the alert delays the postback until the ajax call has had a change to be sent. – Jason P Aug 07 '13 at 14:02
  • @Jason, Is there any way to make JS code idle till the ajax call returns? – amir moradifard Aug 07 '13 at 14:11
  • You could make the ajax call synchronous, but that is deprecated and not really a good idea. The better option would be to restructure your code and use ajax callbacks or jQuery deferred objects. – Jason P Aug 07 '13 at 14:13

0 Answers0