最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Get response from server with Javascript after Javascript request - Stack Overflow

programmeradmin1浏览0评论

My Javascript function request to a aspx page. İts Code:

 var xhr = ("XMLHttpRequest" in window) ? new XMLHttpRequest() : new ActiveXObject("Msxml3.XMLHTTP");
    xhr.open("GET", = '.aspx', true);
    xhr.send(""); 

After this request I want to send a response back from this page and catch it on the client side. How do I do that?

My Javascript function request to a aspx page. İts Code:

 var xhr = ("XMLHttpRequest" in window) ? new XMLHttpRequest() : new ActiveXObject("Msxml3.XMLHTTP");
    xhr.open("GET", = 'http://www.example.net/abc.aspx', true);
    xhr.send(""); 

After this request I want to send a response back from this page and catch it on the client side. How do I do that?

Share Improve this question edited Mar 6, 2012 at 13:13 Waynn Lue 11.4k8 gold badges52 silver badges77 bronze badges asked Jul 7, 2010 at 10:48 MuratMurat 8034 gold badges16 silver badges27 bronze badges 1
  • Murat, you should look at a javascript framework (such as jQuery) to do this. Its cross-browser and fairly straight forward. – Kris Krause Commented Jul 7, 2010 at 10:50
Add a comment  | 

4 Answers 4

Reset to default 7

To get the response from XMLHttpRequest in asynchronous mode (third parameter is true for the open() method) you have to set the onreadystatechange property to a callback function. This function will be called back when the response is ready at the browser:

xhr.open("GET", 'http://www.example.net/abc.aspx', true);
xhr.onreadystatechange = function() {
  if (xhr.readyState === 4)  { 
    var serverResponse = xhr.responseText;
  }
};
xhr.send(null);

You may want to check out the following article for further reading on the topic:

  • AJAX Patterns: XMLHttpRequest Call

You have to do a little more to get it work cross-browser, but once it's done you've got a reusable function, without any library.

// making a call is as simple as this
ajax( "http://www.example.net/abc.aspx", function( data ){
    // do something with the server's response
    alert(data);
});

///////////////////////////////////////////////////////////////////////////////

function getXmlHttpObject() {
    var xmlHttp;
    try {
        // Firefox, Opera 8.0+, Safari
        xmlHttp = new XMLHttpRequest();
    } catch (e) {
        // Internet Explorer
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    if (!xmlHttp) {
        alert("Your browser does not support AJAX!");
    }
    return xmlHttp;
}


function ajax(url, onSuccess, onError) {

    var xmlHttp = getXmlHttpObject();

    xmlHttp.onreadystatechange = function() {
      if (this.readyState === 4) {

            // onSuccess
            if (this.status === 200 && typeof onSuccess == 'function') {
                onSuccess(this.responseText);
            }

            // onError
            else if(typeof onError == 'function') {
                onError();
            }

        }
    };
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);
    return xmlHttp;
}​

To get the response, add a readystatechange event handler function. This will get called back when the response is ready to read:

xhr.onreadystatechange= function() {
    if (this.readyState!==4) return; // not ready yet
    if (this.status===200) { // HTTP 200 OK
        alert(this.responseText);
    } else {
        // server returned an error. Do something with it or ignore it
    }
};
xhr.open('GET', 'http://www.example.net/abc.aspx', true);
xhr.send();

Incidentally:

("XMLHttpRequest" in window)

Whilst in is in general a good way to test whether a property exists, this is one of the very few places where it's not ideal.

The problem is that in IE7+, when the ‘native XMLHttpRequest’ option is turned off, XMLHttpRequest still exists as a property in window, but with the unusable value null. So it's better in this specific case to use a simple truth test, which will allow fallback to ActiveX in the (unlikely) event that this option is disabled:

var xhr= window.XMLHttpRequest? new XMLHttpRequest() : new ActiveXObject('MSXML2.XMLHttp');

If you prefer jQuery use this to make ajax calls:

  const data = { info1: "Data", info2: "Data"};

  const reqUrl = "http://www.example.net/abc.aspx";

  $.ajax({
    type: "POST",
    url: reqUrl,
    data: data,
  })
    .done(function (data, status, jqXHR) {
      // Do your stuff
    })
    .fail(function (data, status, jqXHR) {
      alert("Sorry, can't currently reach Server :(");
    })
    .always(function (data, status, jqXHR) {});
发布评论

评论列表(0)

  1. 暂无评论