Ajax-frontend and backend Interaction

Source: Internet
Author: User

Note: Ajax usesAsync ParametersIt is determined whether it is asynchronous or synchronous, false synchronous, and true asynchronous;

The asynchronous execution order is to first execute the subsequent actions and then execute the code in success;

Synchronization is to first execute the code in success and then execute the subsequent code;

Verification: will the data volume during synchronization be choppy? For example, is the page stuck when a large amount of data is searched from the background?

1 ,(Asynchronous) Method call. Subsequent Code does not need to wait for its execution results.
Background <C #>:

Using system. Web. Script. Services;

[Webmethod]
Public static string getstr (string str1, string str2)
{
Return str1 + str2;
}
 
Foreground <jquery>:

Function Test (strmsg1, strmsg2)
{
$. Ajax ({
Type: "Post ",
URL: "demo. aspx/getstr ",
Async: True,
// Method parameters must be written in the same way as those in the background, case sensitive, cannot be an array, str1 is the name of the form parameter, and str2 is the name of the second form parameter.
Data: "{'str1': '" + strmsg1 + "', 'str2': '" + strmsg2 + "'}",
Contenttype: "application/JSON; charset = UTF-8 ",
Datatype: "JSON ",
Success: function (data ){
// Use data. D to obtain the returned data.
Alert (data. D );
},
Error: function (ERR ){
Alert (ERR );
}
});

// Hide an animation.
$ ("# Pageloading"). Hide ();
}

 

2 ,(Synchronization) Method call, which can be used to obtain the returned value is the prerequisite for executing subsequent code.

Background <C #>:

Using system. Web. Script. Services;

[Webmethod]
Public static string getstr (string str1, string str2)
{
Return str1 + str2;
}
 
Foreground <jquery>:

Function Test (strmsg1, strmsg2)
{

VaR STR = "";
$. Ajax ({
Type: "Post ",
URL: "demo. aspx/getstr ",
Async: false,
// Method parameters must be written in the same way as those in the background, case sensitive, cannot be an array, str1 is the name of the form parameter, and str2 is the name of the second form parameter.
Data: "{'str1': '" + strmsg1 + "', 'str2': '" + strmsg2 + "'}",
Contenttype: "application/JSON; charset = UTF-8 ",
Datatype: "JSON ",
Success: function (data ){
// Use data. D to obtain the returned data.
STR = data. D;
},
Error: function (ERR ){
Alert (ERR );
}
});

Return STR;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.