Why does ajax dynamically assign a value to the href of the a tag without performing redirection? Analysis and Solution

Source: Internet
Author: User

Because the No. 1 evaluation system needs to add a new function "questionnaire", I want to keep the questionnaire prepared by others. The advantage of using others is that they are specialized in other people's work, ui beautification is very user-friendly, but it requires you to publish the website after you set up the questionnaire every time, and the client accesses the changed address every time.

Each time the client clicks "questionnaire", it will jump to the corresponding page:

Click "questionnaire" to go to the questionnaire page:

I use the label to access the "questionnaire" submitted by the Administrator each time"Copy codeThe Code is as follows: <li> <a href = "" onclick = "meizz (this)"> questionnaire </a> </li>

Click "questionnaire" and call the meizz function. The function returns the "questionnaire" URL to be accessed through ajax:Copy codeThe Code is as follows: <script type = "text/javascript" language = "javascript">
Function meizz (e ){
$. Ajax ({
// Async: false, // set ajax Synchronization
Type: "get", // get Method
Url: "../handler/QueryQuestionnaire. ashx ",
// The returned data is successfully parsed and displayed in the course information list.
Success: function (strJson ){
// Check the data returned by the background
Var dataArray = eval (strJson );
E. href = dataArray [0]. Questionnaire; // URL to be accessed
},
// Failed to return data. An error is displayed.
Error: function (XmlHttpRequest, textStatus, errorThrown ){
Alert (XmlHttpRequest. responseText );
}
});
}
</Script>

E. href can be run on $. ajax of the function, but cannot be run if it is bound to the dynamic return address in ajax. After being depressed, I finally found the answer: Because ajax calls are asynchronous by default, instead of assigning values to href for ajax calls to handle callback functions, I directly initiate href, here, href is null.

Solution:Change the ajax call method to synchronous, and declare that the ajax call method is synchronous:Copy codeThe Code is as follows: $. ajax ({
Async: false, // set ajax Synchronization
}

In this way, the href execution will be triggered only after the ajax call in the function is completed, and the desired result is obtained.

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.