Jump to content

ler dados no meu site de outro site


FQueiros

Recommended Posts

  • 4 weeks later...

Cá vai um pequeno exemplo de um livro como usar jquery com asmx webservices ("ASP.NET jQuery Cookbook ISBN 978-1-849690-46-1") :

In addition to page methods, jQuery also enables the making of AJAX calls to web services. In

this recipe, we will validate the username keyed in by the end user by making a call to a web

service instead.

1. Add a web service UserNameWS.asmx to the current project.

2. Add the following namespace to the code-behind:

using System.Web.Services;

3. To enable the web service to be called using AJAX, add the following:

[system.Web.Script.Services.ScriptService]

4. Add a web method to validate the username as follows:

[WebMethod]
public bool CheckUserName(string sUserName)
{
 string[] UserNameArray;
 UserNameArray = new string[7] { "testid01", "testid02",
 "testid03", "testid04", "testid05", "testid06", "testid07" };
 foreach (string strUsername in UserNameArray)
 {
 if (sUserName == strUsername)
   return true;
 }
 return false;
}

5. Add a new web form Recipe4.aspx to the current project.

6. Add a TextBox and a Button control to the form as follows:

<form id="form1" runat="server">
 <div>
   <asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
   <asp:Button ID="btnSubmit" runat="server" Text="Check User Name" />
 </div>
</form>

the complete jQuery solution is as follows:

<script language="javascript" type="text/javascript">
$(document).ready(function() {
 $("#btnSubmit").click(function(e) {
   e.preventDefault();
   if ($("#txtUserName").val() == '')
     alert("Please enter the UserName");
   else
     sendData($("#txtUserName").val());
 });
 function sendData(sUserName) {
   $.ajax({
     type: "POST",
     url: "UserNameWS.asmx/CheckUserName",
     data: '{"sUserName":"' + sUserName + '"}',
     contentType: "application/json; charset=utf-8",
     dataType: "json",
     success: function(msg) {
       if (msg.d)
         alert("The User Name is valid");
       else
         alert("The User Name is invalid")
     },
     error: function() {
       alert("An unexpected error has occurred during processing.");
     }
   });
 }
});
</script>

Espero que seja util.

.NET/T-SQL, JAVA, PHP, Javascript Developer | Business Intelligence | Gestão de Sistemas de Informação Empresariais

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.