Note that there are some explanatory texts on larger screens.

plurals
  1. POjquery nested ajax problem
    primarykey
    data
    text
    <p>I have one jquery ajax call nested inside another. The second call works, but the populated var I'm trying to return on the second call's success winds up undefined in the calling function. Here's the code:</p> <pre><code>$("input#ButtonSubmitMedNameLookup").click(function(evt) { evt.preventDefault(); var dto = { medName: $("#TextBoxMedNameLookup").val() }; $.ajax({ type: "post", contentType: "application/json; charset-utf-8", dataType: "json", url: "/Users/MedicationAddNew.aspx/LookupMed", data: JSON.stringify(dto), success: function(response) { var meds = response.d; writeMedsToTable(meds); } }); </code></pre> <p>...and further down in my codefile...</p> <pre><code>function writeMedsToTable(meds) { var htmlString; if (meds.length &gt; 0) { htmlString = "&lt;h3&gt;Which of these is it?&lt;/h3&gt;"; htmlString += "&lt;table cellpadding=\"0\" cellspacing=\"0\" class=\"jtable\"&gt;&lt;tbody&gt;"; for (i = 0; i &lt; meds.length; i++) { htmlString += "&lt;tr&gt;&lt;td&gt;" + "&lt;input type=\"image\" class=\"newMedSelector\" src=\"Thumb.aspx?img=" + meds[i].RxCui.toString() + "&amp;w=75&amp;h=75\" rxcui=\"" + meds[i].RxCui.toString() + "\"&gt;" + meds[i].BrandName + " " + meds[i].Strength + " " + meds[i].Route + "&lt;/td&gt;&lt;/tr&gt;"; } htmlString += "&lt;/tbody&gt;&lt;/table&gt;"; } else { htmlString = getSuggestionsString(); } $("div#SearchResults").html(htmlString); $("div#SearchResults").css({ padding: "10px", color: "#FF0000" }); $("div#SearchResults").show(500); } function getSuggestionsString() { var dto = { medName: $("#TextBoxMedNameLookup").val() }; $.ajax({ type: "post", contentType: "application/json; charset-utf-8", dataType: "json", url: "/Users/MedicationAddNew.aspx/GetSuggestions", data: JSON.stringify(dto), success: function(response) { var suggestions = response.d; var htmlString = "&lt;h3&gt;No results returned. Did you mean one of these?&lt;/h3&gt;"; htmlString += "&lt;table cellpadding=\"0\" cellspacing=\"0\" class=\"jtable\"&gt;&lt;tbody&gt;"; for (i = 0; i &lt; suggestions.length; i++) { htmlString += "&lt;tr&gt;&lt;td&gt;" + "&lt;a href=\"#\"&gt;" + suggestions[i] + "&lt;/a&gt;" + "&lt;/td&gt;&lt;/tr&gt;"; } htmlString += "&lt;/tbody&gt;&lt;/table&gt;"; return htmlString; // contains expected string } }); } </code></pre> <p>When I step through this in Firebug, htmlString within getSuggestionsString contains what I need right before being returned, but htmlString in writeMedsToTable is undefined after the call. </p> <p>I don't get it. Please, help.</p> <p><strong>EDIT:</strong> Well, I still don't know what the problem is with the above code. But for my purposes, as a workaround, it works if I just to select the target div again like this:</p> <pre><code>function getSuggestionsString() { var dto = { medName: $("#TextBoxMedNameLookup").val() }; $.ajax({ type: "post", contentType: "application/json; charset-utf-8", dataType: "json", url: "/Users/MedicationAddNew.aspx/GetSuggestions", data: JSON.stringify(dto), success: function(response) { var suggestions = response.d; var htmlString = "&lt;h3&gt;No results returned. Did you mean one of these?&lt;/h3&gt;"; htmlString += "&lt;table cellpadding=\"0\" cellspacing=\"0\" class=\"jtable\"&gt;&lt;tbody&gt;"; for (i = 0; i &lt; suggestions.length; i++) { htmlString += "&lt;tr&gt;&lt;td&gt;" + "&lt;a href=\"#\"&gt;" + suggestions[i] + "&lt;/a&gt;" + "&lt;/td&gt;&lt;/tr&gt;"; } htmlString += "&lt;/tbody&gt;&lt;/table&gt;"; $("div#SearchResults").html(htmlString); } }); } </code></pre> <p>Not my ideal, but it works.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload