Home  >  Article  >  Web Front-end  >  jquery calls the asp.net page background implementation code_jquery

jquery calls the asp.net page background implementation code_jquery

WBOY
WBOYOriginal
2016-05-16 18:07:22903browse

First create an aspx page and write a client control value="AjaxDemo">
Then write a simple method on the aspx background page, the code is as follows:

Copy code The code is as follows:

[WebMethod]
public static string ABC(string ABC)
{
return ABC;
}

Must be declared as static methods, and they must be annotated with the [WebMethod] attribute. But in webservice, it does not have to be a static method. Its objects can be list, dataset, class objects, etc...
Next, you should consider how to let the client control in the foreground call the method in the background. This is when jqury comes on the scene. Alright..
Introduce jquery class library into the page

Add the script code to the page as follows:
Copy the code The code is as follows:

< script type="text/javascript">
$().ready( function() {
  $("#AjaxDemo").click(function() {
  $.ajax({
type: "POST",
url: "Default.aspx/ABC",
data: "{'ABC':'test'}",
contentType: "application/json; charset=utf -8",
success: function(msg) {alert(msg); }
})
})
}
)


That’s it! Many things are done for us by the jquery class library. What we discuss here is how to use it and how to implement it. We don’t care!
There are many things like $ in jquery Methods such as .ajax are provided for us to use. You can try it!
Supplement: Pay attention to building 3.5 projects, if it is 2.0. The configuration file will be referenced much less if you are building a 2.0 project. Build a 3.5. Just overwrite the 3.5 configuration file to the 2.0 project!
If the above code pops up "{d:test}" after success, it is because it returns a string, we can change it to return a json object.
Modify the jquery code as follows
Copy code The code is as follows:

$().ready(
function() {
$("#AjaxDemo").click(function() {
 $.ajax({
type: "POST",
 url: "Default.aspx/ABC",
data: "{'ABC':'test'}",
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function(msg) {alert( msg.d); }
})
})
}
}

We set the data it returns to be a json object, and now we can use the returned json Object, according to the d:test that pops up, we can clearly see that the key is d and the value is test. Then we use the returned data msg object to directly click d to get the test. After modifying the code. Now it pops up It's just a test...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn