2

whats hapenning is that my ajax request randomly fails, and I don't know why.

I've been testing only in chrome and when the error callback gets called the controller returns successfully so i think that the problem is not in the server side.but i'm not sure. the responseText error in chrome is blank so i have no tip to troubleshoot.

This is My Ajax call m I doing somehting wrong, I'm Clueless?

$.ajax({
    type: "GET",
    url: url,
    data: { postalCode: postalCode },
    dataType: "json",
    success: function (response) {
        if (isPostBack != 'True') {
            switch (response["Code"]) {
                case "-1":
                    alert('msg 1.');
                    break;
                case "0":
                    alert('msg 2.');
                    break;
                case "1":
                    alert('msg 3.');
                    break;
                case "2":
                    alert('msg 4.');
                    break;
                default:
                    alert('unexpected value.');
            }
        }
    }
});

if not what could be the most likely causes? I'm Developing Asp.NET MVC for Sitefinity, and I only detect this issue in this ajax request.

UPDATE:

I've detected in the browser that the request is being cancelled. it arrives successfully to the server and is cancelled during the code execution. it is not cancelled in a specific line because I commented the lines to find which one is causing troubles but it was cancelled regardless of the code line. Then I started thinking about timeout and added a timeout. first 3 seconds than 10 seconds. but the problem was still there. this is the request status:

enter image description here

Nelssen
  • 1,023
  • 1
  • 17
  • 42
  • 3
    Try adding an error in the ajax to see if you have output there – Víctor Sep 11 '15 at 08:52
  • also, try to check your server logs, there're bound to be some kind of error or at least access log for this to ensure you're really getting through on the server side – Zathrus Writer Sep 11 '15 at 09:00
  • Hi Del, The error callback does not change the result, I'm still getting the error. Zathrus I'm not seeing nothing in the server logs. However i made a new finding, the Ajax request is being cancelled. I'll Update the thread – Nelssen Sep 12 '15 at 19:05
  • Show us the code in the controller. Is it an Web API controller? Have you defined a custom route for it in Sitefinity? – Veselin Vasilev Sep 14 '15 at 01:49

2 Answers2

1

Suggesting a slight modification:

$.getJSON(url, {"postalCode": postalCode})
 .success(function (response) {
    if (isPostBack != 'True') {
        switch (response.Code) {
        case "-1":
            alert('msg 1.');
            break;
        case "0":
            alert('msg 2.');
            break;
        case "1":
            alert('msg 3.');
            break;
        case "2":
            alert('msg 4.');
            break;
        default:
            alert('unexpected value.');
        }
    }
});
ankhzet
  • 2,517
  • 1
  • 24
  • 31
  • Hi ankhzet, thanks for presenting me this way of doing ajax request. however the result is the same. I made a new finding the request are beeing cancelled, I'll update the thread. – Nelssen Sep 12 '15 at 19:03
  • show source code where you processing that `Get`, Is it recursively redirects to the same `/Get` location, so Chrome cancels it? – ankhzet Sep 12 '15 at 20:04
  • 1
    try `chrome://net-internals/#events` link for complete trace of ajax call – ankhzet Sep 12 '15 at 20:06
  • Its a very nice Tool! – Nelssen Sep 16 '15 at 16:20
1

The Problem

I had the ajax request been made from a submit button, which cancelled some of the requests.

I solve it by

  • hiding the submit button
  • creating a button type=button that does the ajax call
  • on success callback i click the hidden button to submit the form values $("#btnsubmit").click();

After this I still had a problem, the first request was always cancelled. The problem was timeout. when I increased the timeout value to 20 seconds it stops from being cancelled. but leads me to another problem.

why it needed 20 seconds in the first ajax request?

Well that will be another Adventure..

Nelssen
  • 1,023
  • 1
  • 17
  • 42
  • `on success callback i click the hidden button`... you can just `submit` the form either by `$('form.some-form-selector').submit()` or by `document.forms[n].submit()`... – ankhzet Sep 21 '15 at 04:19