0

On my Page I've got DropDownList, Date Picker and ASP.NET Button.

When I pick up date and something from list, after click, it should show Google Map with few markers. I'm using Web Method to find records in data base filtered by items from list and datepicker.

When I'm trying to get these records from WebMethod I used AJAX which returns good JSON, but I've got error: Too much recursion.

I tried this JS code:

function myFun() {
    DrawMap();
    var SelectedItem = document.getElementById("ctl00_PageContent_DropDownList").value;
    var SelectedDate = document.getElementById("ctl00_PageContent_DatePicker").value;
    PageMethods.MyWebMethod(CarReg, SelectedDate, function (result) {
        parsed = JSON.parse(result);
    });

Where function(result){ parsed = JSON.parse(result); assigns result to global variable parsed.

In my button I've got attribute OnClientClick with value myFun(); return false; because after postback my map doesn't work. It works, but I have to click twice.

I noticed that when I do alert(parsed); after call MyWebMethod I got undefined.

Why parsed is undefined after assign result?

urbz
  • 2,663
  • 1
  • 19
  • 29
user3692826
  • 107
  • 1
  • 11
  • can you show your `MyWebMethod` – Dgan Oct 02 '14 at 11:44
  • `myFun` is executed asynchronously, which means that by the time the browser finishes it's execution, the `AJAX` request (`WebMethod`) didn't got the answer from the server. You should use `alert(parsed)` in the callback function of the `WebMethod` – Catalin Oct 02 '14 at 13:33
  • @RaraituL Okay, so why my global variable **parsed** doesn't assign result of the **WebMethod**'s callback? _function(result){ parsed = JSON.parse(result);_ should do everything, but it works only by clicking twice. When I make variable **parsed** locally in _myFun_ it won't run. – user3692826 Oct 02 '14 at 15:03
  • @user3692826: It does assign the result to variable `parsed`, but only after the `AJAX` request has ended, which is **after** `myFun` has finished it's execution. It is a simply coincidence that it works the second time, because, by the time you execute it the 2nd time, the 1st `AJAX` request got the answer from the server (and assigned the variable `parsed`). Use [console.log()](http://stackoverflow.com/questions/4539253/what-is-console-log) for debugging and you will see – Catalin Oct 03 '14 at 06:15

0 Answers0