I have a problem regarding with asp.net web service.I have a register.aspx page. I want to do thing,if there is already a record which entry to the inside of text field during the process of register,I want to give a alert to me.
I'm using jquery for that.I wrote those codes by creating web service.However,it can't find to the webservice.What should I do ?
Here is my code.
Web service:
[WebMethod]
public int CheckUser(string username)
{
string constr = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;
string query = String.Format("select COUNT(*) from Users where Username='{0}'", username);
SqlConnection baglan = new SqlConnection(constr);
SqlCommand cmd = new SqlCommand(query, baglan);
baglan.Open();
int result = (int)cmd.ExecuteScalar();
baglan.Close();
return result;
}
Register.aspx:
<script src="jquery-1.6.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
$("#Txtuname").change(CheckUser);
});
function CheckUser() {
//alert("test");
$.ajax({
type: "POST",
url: "WebService1/Service1.asmx/CheckUser",
data: "{username: '" + $('#Txtuname').val() + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
$("#duplicate").empty();
if (response.d != "0") {
$("#duplicate").html(' That user name has already been taken');
}
}
});
}
</script>
Related Field:
<tr>
<td>
<span class="label">Username:</span>
</td>
开发者_高级运维 <td>
<asp:TextBox ID="Txtuname" runat="server"></asp:TextBox><span id="duplicate"></span>
</td>
</tr>
Use /WebService1/Service1.asmx/CheckUser
if your web service is located at the root of your application. Otherwise, please show us the file-system structure of your project.
Hint: Always copy the path of the current web service, remove the method name and append it to the end of the URL. If it's found, then everything is OK. For example, in your case, follow these steps:
- Grab the web service path:
/WebService1/Service1.asmx/CheckUser
- Remove the method name from it:
/WebService1/Service1.asmx
- If it starts with /, simply append it to the root path of your site:
http://www.site.com/WebService1/Service1.asmx
otherwise, append it to the current path of your current URL (URL of your current page)http://www.site.com/pageName.aspx/WebService1/Service1.asmx
- If it's resolved, then the path is OK.
精彩评论