Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6092843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:29:30+00:00 2026-05-23T12:29:30+00:00

this code always return 0: function myFunc() { var str = 0; JQuery.ajax({ type:

  • 0

this code always return “0”:

function myFunc() {

var str = "0";

JQuery.ajax({
      type: "POST",
      url: "test.aspx/testMethod",
      data: "{}",
      dataType: "json",
      contentType: "application/json; charset=utf-8",
      success: function(msg) {

       alert(str); //here shows my value from my method
       str = msg.d;

      }

});

alert(str); //here shows "0";

return str;

But the return value of my js function is always “0”.
I think the JavaScript returns str before the ajax is completed. What I could do?

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T12:29:31+00:00Added an answer on May 23, 2026 at 12:29 pm

    You are correct. Alert is being called before you hear back from the server. If you don’t want the .ajax call to return immediately, you can set async to false. Does this work?

    function myFunc() {
    
    var str = "0";
    
    JQuery.ajax({
          type: "POST",
          async: false,
          url: "test.aspx/testMethod",
          data: "{}",
          dataType: "json",
          contentType: "application/json; charset=utf-8",
          success: function(msg) {
    
           alert(str); //here shows my value from my method
           str = msg.d;
    
          }
    
    });
    
    alert(str); //here shows "0";
    
    return str;
    }
    

    I should point out that the way you are doing this probably isn’t the most appropriate. You are trying to create a function which works syncronously because that is simple and it’s what you are used to. However, it defeats the whole purpose of asynchronous requests, and doesn’t fit the paradigms of Javascript well. Here’s what the jQuery documentation has to say about this:

    Note that synchronous requests may temporarily lock the browser, disabling any actions while the request is active.

    It would be better to have a callback function which uses the value that you get back from your request.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I always see the code like this in the blogs: $.ajax({ type: POST, contentType:
Why does this code always return 0? var possibleMatches = new Array(); $.getJSON('getInformation.php', function(data)
I used this code to read file. But fread function always return 0. What
This code always works, even in different browsers: function fooCheck() { alert(internalFoo()); // We
Having the following code class test { private $name; public function __get($name){ return $name;
this code always returns 0 in PHP 5.2.5 for microseconds: <?php $dt = new
Considering this code, can I be absolutely sure that the finally block always executes,
I always have this notion that writing SQL queries in the code behind is
This code is always alerting out null , which means that the string does
I have this code, what am I doing wrong? I have a function which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.