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 8457597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:50:55+00:00 2026-06-10T12:50:55+00:00

I have one js file having code as: function postResponse(url1,param1) { var url =

  • 0

I have one js file having code as:

function postResponse(url1,param1)
{
var url = intranetUrl + encodeURI(url1);

 var xhr = new XMLHttpRequest();
    xhr.open('POST', url, true);
    xhr.onload = function(e)
    {
        if (this.status == 200)
        {
            genericRes = this.responseText;
            console.log("inside fun:"+genericRes);
            return genericRes;
        }
        alert("!!!"+this.status);
    };

    xhr.send(param1);


}

Now from another file I want to access this function I had imported above file in this file and call function as:

<script type="text/javascript">

        var resss = postResponse("myURL","ACC|^ALL|^M|^|$");
        alert("genericRes"+genericRes);
        console.log("genericRes>>>"+genericRes);
        console.log("resss>>>"+resss); 

    </script>

But here I got genericRes and resss value as undefined, and above console.log prints first and then it prints console.log("inside fun:"+genericRes); here I got correct output, But from calling code it gives me undefined.

In java we write suppose method which can return String as:

public String myMethod()
{
      str = "MyString";
      return str;
}

and call that method as:

String str1 = myMethod();

But how to do this in jquery?

Any suggestion will be appreciated. Thanks in advance

  • 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-06-10T12:50:56+00:00Added an answer on June 10, 2026 at 12:50 pm

    If you look carefully, you are defining another function, as in:

    function(e) //<-- this is the another function
    {
        if (this.status == 200)
        {
            var genericRes = this.responseText;
            console.log("inside fun:"+genericRes);
            return genericRes; //<-- this only applies to this function
        }
        alert("!!!"+this.status);
    };
    

    So it will return that value to the caller of xhr.onload, which is the browser and the browser doesn’t do anything with the return value.

    Furthermore, you cannot really return from asynchronous operations, you have to use a callback.

    So:

    function postResponse(url1, param1, callback) { // <-- take a callback parameter
        var url = intranetUrl + encodeURI(url1);
        var xhr = new XMLHttpRequest();
        xhr.open('POST', url, true);
        xhr.onload = function(e) {
            if (this.status == 200) {
                var genericRes = this.responseText;
                callback( genericRes ); // <-- call the callback
            }
        };
        xhr.send(param1);
    }
    

    Then in your code:

    postResponse("myURL", "ACC|^ALL|^M|^|$", function(result) {
        alert(result); //Result is only available here. In this anonymous function defined inline. Yes.
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a *.txt files having integers, one on each line. So the file
Ok, so I have a file called functions.ps1 that contains only function code: something
i have default.aspx page and one user control. usercontrol is having following code for
I have one file in my project, (the readme file,) which I would like
I have one file (for example: test.txt), this file contains some lines and for
I have problem with session in cakephp.I have one file chat.php that is in
I want to create a 'mini-database'. I want to have one file with lots
I have one flash file which is embading in another, the embeded one is
I have one single file called Exercises.rb def ask(prompt) print prompt, ' ' $stdout.flush
I have one xsd file I'd rather not modify (Exceptions.xsd): <xs:schema xmlns:xs=http://www.w3.org/2001/XMLSchema xmlns=http://me.com/Exceptions.xsd targetNamespace=http://me.com/Exceptions.xsd

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.