使用javascript调用Web服务时出现问题 [英] Problem in calling web services using javascript
问题描述
当我尝试调用我的Web服务方法时,即:
When i am trying to call my web service method ie:
public Sample () {
[WebMethod]
public string CheckUserNameAvailability(string userName)
{
SqlConnection cn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Connect"].ConnectionString);
SqlDataAdapter dap = new SqlDataAdapter("select Email_Id from User_Registration where Email_Id='" + userName + "'", cn);
DataSet ds = new DataSet();
dap.Fill(ds);
if (ds.Tables[0].Rows.Count != 0)
{
return "Name Already Exist";
}
else
{
return "Available";
}
}
}
然后我尝试使用脚本管理器(即
)对此方法进行引用.
then I am trying to make reference of this method using scripts manager ie.
<asp:ScriptManager ID="ScriptManager1" runat="server">
<services>
<asp:ServiceReference Path="~/Sample.asmx" />
</services>
但是当我在javascript函数中调用我们的方法时,即使我正在使用<%@ import Namespace ="System.Web.Services"%>在aspx页面顶部
错误:-更新JScript Intellisense时出错
请给我解决方案,让我可以轻松地在.aspx页面中访问类.
but when i calling we method in javascript function then it can''t be accessible there even i am using <%@import Namespace="System.Web.Services"%> on the top of aspx page
error:-Error updating JScript Intellisense
please give me the solution how cal i access my class easily in my .aspx page
推荐答案
使用ajax调用来访问Web服务.您需要立即将您的WebMethod设为静态
ajax调用的工作方式如下:
Hi,
Use ajax call to hit the Webservice. you need to make your WebMethod static now
ajax call works like this:
function CheckUserNameAvailability(username) {
try {
.ajax({ 类型:" , 网址:" , 数据:" +用户名+ " , contentType:" , dataType:" , 成功:功能(msg){ 如果(msg.d == " span>){ alert(' 可用..........'); } }, 错误:功能(){ alert(' 发生错误!'); } }); } 捕获(e){ 警报(e); } }
.ajax({ type: "POST", url: "YourForm.aspx/CheckUserNameAvailability", data: "{'userName':'" + username + "'}", contentType: "application/json; charset=utf-8", dataType: "json", success: function (msg) { if (msg.d == "Available") { alert('Available..........'); } }, error: function () { alert('Error occured!'); } }); } catch (e) { alert(e); } }
在这里,将URL的页面名称替换为YourForm.aspx.
当您在javascript中以
调用方法时,将值发送给参数用户名
Here replace your page name with YourForm.aspx in the url.
send value to parameter username when you call the Method in javascript as
<asp:button id="btnCheckName" runat="server" onclientclick="return CheckUserNameAvailability('username');" xmlns:asp="#unknown" />
这篇关于使用javascript调用Web服务时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!