0

I just created an Ajax method. It works if I don't pass any data to the controller. If I want to pass some data to the controller. It works only if I open the IE degguer. I tried to add the cache option to false. But it does not work. Here is some of my code :

function ReportIdLoad(reportId) {
   $.ajax({
        type: 'POST',
        cache: false,
        url: 'WebReportGeneratorView.aspx/ReportIdLoad',
        contentType: 'application/json',
        data: JSON.stringify(reportId),
        dataType: 'json'
    })
}

[WebMethod]
public static void ReloadReportWorkbookContext(string reportId)
{
   //My code
}

Thanks in advance for your help.

1 Answers1

0

@Roamer-1888 :

Thanks for the track.

Indeed, It sounds like this problem.

I add the option :

async: false

And I get to my break point.

  • Ludovic, that proves the point but isn't a great solution. You should really omit `async:false`, return a promise from ` ReportIdLoad()`, and chain `ReportIdLoad(...).then(...)` wherever it is called. – Roamer-1888 Jun 06 '17 at 19:09
  • You might like to read [this](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call). There's plenty to take on board but it will be a good investment. – Roamer-1888 Jun 07 '17 at 12:59
  • I think I don't need the then. Because I don't expect a return coming from the Ajax query. The controller does a processing without needing to return something to the ajax function. no ? (This is my full comment sorry enter keyboard error) – Ludovic Roux Jun 07 '17 at 13:05
  • But there must be something which is dependent on the ajax completing. Otherwise there wouldn't have been a problem in the first place, and `async:false` wouldn't have had any affect. – Roamer-1888 Jun 07 '17 at 13:12
  • Would not it come from the fact that I call my method in a window.onbeforeunload? And that the window closes before the end of the Ajax query and that it is lost? `window.onbeforeunload = function (event) { var reportId = { "reportId": $("#HiddenReportId").val() }; ReportIdLoad(reportId); };` – Ludovic Roux Jun 07 '17 at 13:18
  • Used that way, you have "fire-and-forget" ajax. There's no attempt to wait for the ajax response before the page unloads, therefore nothing to inhibit the page unloading if the ajax should fail, and no opportunity to warn the user. I'm not sure that's something I would ever do. – Roamer-1888 Jun 07 '17 at 14:17