J'utilise le code suivant pour obtenir le temps du serveur.jquery.ajax ne fonctionne pas pour asp: bouton
La fonction btnNoP .click renvoie l'heure correcte, cependant le paramètre asp: Button renvoie l'erreur Error sur la page.
Comment puis-je appeler avec succès un webmethod de document.ready lorsqu'un asp: clique sur le bouton?
<script type="text/javascript"> function PageMethod(fn, paramArray, successFn, errorFn) { var pagePath = window.location.pathname; //Create list of parameters in the form : {"paramName1":"paramValue1","paramName2":"paramValue2"} var paramList = ''; if (paramArray.length > 0) { for (var i=0; i<paramArray.length; i+=2) { if (paramList.length > 0) paramList += ','; paramList += '"' + paramArray[i] + '":"' + paramArray[i+1] + '"'; } } paramList = '{' + paramList + '}'; alert(pagePath + "/" + fn); //Call the page method $.ajax({ type: "POST", url: pagePath + "/" + fn, contentType: "application/json; charset=utf-8", data: paramList, dataType: "json", success: successFn, error: errorFn }); } function AjaxSucceeded (result) { alert(result.d); //$("#Result").text("Result : " + result.d); } function AjaxFailed (result) { alert("Error on Page"); } $(document).ready(function() { $('#btnNoP').click(function() { PageMethod("GetTime", [], AjaxSucceeded, AjaxFailed); }); $('#btnNoParams').click(function() { PageMethod("GetTime", [], AjaxSucceeded, AjaxFailed); }); }); </script>
<asp:ScriptManager ID="sm" EnablePageMethods="true" EnablePartialRendering="true" runat="server"></asp:ScriptManager> <asp:Button ID="btnNoParams" Text="Get Time" runat="server" /> <input type="button" id="btnNoP" />
[WebMethod(true)] public static string GetTime() { return "You called at : " + DateTime.Now.ToShortDateString() + "-" + DateTime.Now.ToShortTimeString(); }
Si vous pouvez montrer ce que le code HTML généré pour le bouton ressemble (plutôt que la source de la page ASP), vous trouverez probablement plus de gens qui pourraient aider car il ne nécessiterait pas de savoir quoi que ce soit au sujet ASP, juste HTML/JS . – jfriend00