无法第二次调用Web服务来自jQuery(Unable to call web service 2nd time From jQuery)

编程入门 行业动态 更新时间:2024-10-28 18:28:54
无法第二次调用Web服务来自jQuery(Unable to call web service 2nd time From jQuery)

我无法从jQuery函数代码第二次调用Web服务。

页:

<telerik:RadCodeBlock ID="RadCodeBlock1" runat="server"> <script type="text/javascript"> function getCityByPinCode() { var path = '<%=ConfigurationManager.AppSettings["AutoComplete"].ToString() %>'; $.ajax({ type: "POST", url: "" + path + "/CityBasedOnPinCode", data: "{pincode: '" + $("#<%=ntxbPostCode.ClientID%>").val() + "'}", contentType: "application/json; charset=utf-8", dataType: "json", success: OnSuccess, error: OnError }); } function OnSuccess(data, status) { var city = document.getElementById('<%= tbxCity.ClientID %>'); var val1 = data.d; document.getElementById("ContentPlaceHolder1_CompanyDetails1_tbxCity").value = val1; } function OnError(request, status, error) { alert(request.statusText); } </script> </telerik:RadCodeBlock> <body> <telerik:RadNumericTextBox ID="ntxbPostCode" runat="server" DataType="System.Int32" MaxLength="4" TabIndex="4" Width="95%" DecimalDigits="0" ClientEvents-OnValueChanged="getCityByPinCode()"> </telerik:RadNumericTextBox> <asp:TextBox ID="tbxCity" runat="server" Width="95%" TabIndex="5"></asp:TextBox> </body>

网络服务:

public string CityBasedOnPinCode(string pincode) { int pcode = 0; int.TryParse(pincode, out pcode); List<usp_CityBasedOnPinCodeResult> lstCity = new CityDA().CityBasedOnPincode(pcode); return lstCity[0].CityName.ToString(); }

我想在输入密码的基础上填写城市文本框。所以当用户输入密码时,他/她会自动获取城市名称。当我预先设定密码的值时,它给我完美的结果但是当我输入新值时我得到了错误如下

Microsoft JScript运行时错误:预期的对象。

请注意,如果我在页面加载时(文本框)中有文本框中的密码值,那么我会在更改文本框的值后获得正确的值但不会获得值。 jQuery包含在母版页中。 所以不包括在这里。

I am unable to call web service 2nd time from jQuery function code is here.

Page:

<telerik:RadCodeBlock ID="RadCodeBlock1" runat="server"> <script type="text/javascript"> function getCityByPinCode() { var path = '<%=ConfigurationManager.AppSettings["AutoComplete"].ToString() %>'; $.ajax({ type: "POST", url: "" + path + "/CityBasedOnPinCode", data: "{pincode: '" + $("#<%=ntxbPostCode.ClientID%>").val() + "'}", contentType: "application/json; charset=utf-8", dataType: "json", success: OnSuccess, error: OnError }); } function OnSuccess(data, status) { var city = document.getElementById('<%= tbxCity.ClientID %>'); var val1 = data.d; document.getElementById("ContentPlaceHolder1_CompanyDetails1_tbxCity").value = val1; } function OnError(request, status, error) { alert(request.statusText); } </script> </telerik:RadCodeBlock> <body> <telerik:RadNumericTextBox ID="ntxbPostCode" runat="server" DataType="System.Int32" MaxLength="4" TabIndex="4" Width="95%" DecimalDigits="0" ClientEvents-OnValueChanged="getCityByPinCode()"> </telerik:RadNumericTextBox> <asp:TextBox ID="tbxCity" runat="server" Width="95%" TabIndex="5"></asp:TextBox> </body>

Web service:

public string CityBasedOnPinCode(string pincode) { int pcode = 0; int.TryParse(pincode, out pcode); List<usp_CityBasedOnPinCodeResult> lstCity = new CityDA().CityBasedOnPincode(pcode); return lstCity[0].CityName.ToString(); }

I want to fill city textbox on the base of pincode entered.So when User Enters pincode,he/she gets city name automatically.When I put value of pincode predefined it gives me perfect result but when i am entering new value i got error as follows

Microsoft JScript runtime error: Object expected.

Note that if i have value of pincode in textbox at page load time(edit time) Then i am getting proper value but not getting value after changing value of textbox. jQuery is included in masterpage. so not included here.

最满意答案

由于您订阅了RadNumericTextBox提供的客户端事件,因此在代码中使用事件参数可能更容易,而不是使用选择器来获取值。 您可以在此处查看如何从OnValueChanged事件获取新值的示例。

除此之外,当我将代码更改为以下内容时,我的一切工作正常:

<script type="text/javascript"> function getCityByPinCode(sender, eventArgs) { //do stuff } </script> <telerik:RadNumericTextBox ID="ntxbPostCode" runat="server" DataType="System.Int32" MaxLength="4" TabIndex="4" Width="95%" DecimalDigits="0" ClientEvents-OnValueChanged="getCityByPinCode"> </telerik:RadNumericTextBox> <br /> <asp:TextBox ID="tbxCity" runat="server" Width="95%" TabIndex="5"></asp:TextBox>

这可以防止事件在初始页面加载时触发(与代码一起),并确保我设法使用get_newValue()函数获取RadNumericTextBox的新值。

小注意:您应该能够在OnSuccess函数中使用city变量来设置其值,而不是再次调用getElementbyID。

Since you are subscribing to the client-side event offered by the RadNumericTextBox it might be easier to use the event arguments in your code as opposed to using a selector to get a hold of the value. You can see an example of how to get the new value from the OnValueChanged event right here.

Along with this, when I changed the code to the following everything worked fine on my end:

<script type="text/javascript"> function getCityByPinCode(sender, eventArgs) { //do stuff } </script> <telerik:RadNumericTextBox ID="ntxbPostCode" runat="server" DataType="System.Int32" MaxLength="4" TabIndex="4" Width="95%" DecimalDigits="0" ClientEvents-OnValueChanged="getCityByPinCode"> </telerik:RadNumericTextBox> <br /> <asp:TextBox ID="tbxCity" runat="server" Width="95%" TabIndex="5"></asp:TextBox>

This prevented the event from firing on initial page load (which it was with your code) and also ensure that I managed to get the new value of the RadNumericTextBox with the get_newValue() function.

Small note: you should be able to just use the city variable in the OnSuccess function to set its value, as opposed to calling getElementbyID again.

更多推荐

本文发布于:2023-08-03 15:08:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1393171.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Unable   jQuery   Web   call   time

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!