jQuery API

jQuery.get()

jQuery.get( url, [data,] [success(data, textStatus, jqXHR),] [dataType] ) Returns: jqXHR

Description: Load data from the server using a HTTP GET request.

  • version added: 1.0jQuery.get( url, [data,] [success(data, textStatus, jqXHR),] [dataType] )

    urlA string containing the URL to which the request is sent.

    dataA map or string that is sent to the server with the request.

    success(data, textStatus, jqXHR)A callback function that is executed if the request succeeds.

    dataTypeThe type of data expected from the server. Default: Intelligent Guess (xml, json, script, or html).

This is a shorthand Ajax function, which is equivalent to:

$.ajax({
  url: url,
  data: data,
  success: success,
  dataType: dataType
});

The success callback function is passed the returned data, which will be an XML root element, text string, JavaScript file, or JSON object, depending on the MIME type of the response. It is also passed the text status of the response.

As of jQuery 1.5, the success callback function is also passed a "jqXHR" object (in jQuery 1.4, it was passed the XMLHttpRequest object). However, since JSONP and cross-domain GET requests do not use XHR, in those cases the (j)XHR and textStatus parameters passed to the success callback are undefined.

Most implementations will specify a success handler:

$.get('ajax/test.html', function(data) {
  $('.result').html(data);
  alert('Load was performed.');
});

This example fetches the requested HTML snippet and inserts it on the page.

The jqXHR Object

As of jQuery 1.5, all of jQuery's Ajax methods return a superset of the XMLHTTPRequest object. This jQuery XHR object, or "jqXHR," returned by $.get() implements the Promise interface, giving it all the properties, methods, and behavior of a Promise (see Deferred object for more information). For convenience and consistency with the callback names used by $.ajax(), it provides .error(), .success(), and .complete() methods. These methods take a function argument that is called when the request terminates, and the function receives the same arguments as the correspondingly-named $.ajax() callback.

The Promise interface in jQuery 1.5 also allows jQuery's Ajax methods, including $.get(), to chain multiple .success(), .complete(), and .error() callbacks on a single request, and even to assign these callbacks after the request may have completed. If the request is already complete, the callback is fired immediately.

// Assign handlers immediately after making the request,
  // and remember the jqxhr object for this request
  var jqxhr = $.get("example.php", function() {
    alert("success");
  })
  .success(function() { alert("second success"); })
  .error(function() { alert("error"); })
  .complete(function() { alert("complete"); });

  // perform other work here ...

  // Set another completion function for the request above
  jqxhr.complete(function(){ alert("second complete"); });

Additional Notes:

  • Due to browser security restrictions, most "Ajax" requests are subject to the same origin policy; the request can not successfully retrieve data from a different domain, subdomain, or protocol.
  • If a request with jQuery.get() returns an error code, it will fail silently unless the script has also called the global .ajaxError() method or. As of jQuery 1.5, the .error() method of the jqXHR object returned by jQuery.get() is also available for error handling.
  • Script and JSONP requests are not subject to the same origin policy restrictions.

Examples:

Example: Request the test.php page, but ignore the return results.

$.get("test.php");

Example: Request the test.php page and send some additional data along (while still ignoring the return results).

$.get("test.php", { name: "John", time: "2pm" } );

Example: pass arrays of data to the server (while still ignoring the return results).

$.get("test.php", { 'choices[]': ["Jon", "Susan"]} );

Example: Alert out the results from requesting test.php (HTML or XML, depending on what was returned).

$.get("test.php", function(data){
alert("Data Loaded: " + data);
});

Example: Alert out the results from requesting test.cgi with an additional payload of data (HTML or XML, depending on what was returned).

$.get("test.cgi", { name: "John", time: "2pm" },
   function(data){
     alert("Data Loaded: " + data);
   });

Example: Gets the test.php page contents which has been returned in json format (<?php echo json_encode(array("name"=>"John","time"=>"2pm")); ?>).

$.get("test.php", { "func": "getNameAndTime" },
   function(data){
     alert(data.name); // John
     console.log(data.time); //  2pm
   }, "json");

Support and Contributions

Need help with jQuery.get() or have a question about it? Visit the jQuery Forum or the #jquery channel on irc.freenode.net.

Think you've discovered a jQuery bug related to jQuery.get()? Report it to the jQuery core team.

Found a problem with this documentation? Report it to the jQuery API team.

* All fields are required
  • Saud Prakash
    hi, i am new to jquery, i made a get request like this $.get("get_items_by_simplesearch.php",{search_for:"",near_place:""} ,
    function(result){ $(".right_section_block").html(result); }); only the text is rendering, the bing image of the get_items_by_simplesearch.php page is not rendering, can some one help me please.
  • Hi guys,
    i want to get string in the middle of circle:
    http://www.magick.cz/spiral/ho...
    what method i should use? ... load()? get()? ajax()?
    i'd tried many ways but without success =(

    $.get("http://www.magick.cz/spiral/ho...",
    { den: "1", mesic: "2",rok: "2000" },
    function(data) { $("#horo").text(data) }); // also doesn't get nothing
  • Yogesh gandhi
    I am doing this....



    function downloadFileAndRefreshCart(downloadUrl) {
    $.get(
    url: downloadUrl,
    function(data) {
    // Now that we executed the action corresponding to the downloadUrl
    // Just reload myself (i.e. the iframe)
    self.location.reload();
    }
    );
    }


    But...what is happening is, it is able to download the file, but it doesn't asks the user, where to save the downloaded file, it saves in a temporary location.

    How do I do this? so that the downloaded file is visible on the browser where it is getting saved.
  • leavetheherd
    OK, I got lost somewhere In this code, from the first example in the documentation,

    $.get('ajax/test.html', function(data) {
    $('.result').html(data);
    alert('Load was performed.');
    });

    Lets say I want to insert the html from the external file in a <div> on the page. How do I do that? </div>
  • Franquis
    Hi!
    $('#myDiv').html(data) will insert the var data (the html from 'ajax/test.html') in the div "myDiv"...

    Otherwise, have a look on the load() function...
    i.e. $('#myDiv').load('ajax/test.html');
  • How to hide from the user php file, which is transmitted GET-request???
  • No possibility to do so. Otherwise you have to use FLEX / Flash solution with SSL so even sniffing won't be possible.
    HTTP is as open as possible, you can not "hide" such things.
    Only workaround would be encrypting different things so it isn't obvious on first sight, that you access "xload.php"
  • Guest
    Hi All,

    Please help me with sending 'content-type' in jQuery.get().
    I have found that by default 'application/x-www-form-urlencoded' will be sent through request.
    Even the default value I am not able to see in my code when I try to print 'ContentType' in request.

    Thanks in Advance.
  • Guest
    I want to send content-type in request header while calling Ajax url.
    Its going as a request parameter, not as a request header.
    Please help.
  • Bucki
    // I like jelly and php.

    print "I like Jelly";
    ?>
  • Jq-primaryNavigation
    jq-primaryNavigation
  • Fee
  • jc
    im confuse can you tell me whats is the main difference between $.ajax() and $.get()
  • cs
    $.get is just a shorthanded form of $.ajax specifically for dealing with HTTP GETs.
  • Raghunv
    Hi ,
    I am having a weird issue.. In the original request which is post I Create a file in the server. In the call back I open a popup and attempt to download the file ..I am able to download the file in non IE browsers but not able to do it in IE ... Can you tell me how do i address this issue..
  • Can you please send me the file, how did you achieve this using ajax.

    Actually I have a requirement that there is a zip file on the server which is sent in the response when a particular action is invoked.

    I want that when that action is invoked via ajax, the returned content shows the user a popuup to save that file anywhere he likes. But that is not coming, as the request goes in the background. Or I am using it in a wrong manner.

    I tried http://www.filamentgroup.com/l...

    But that is also not workign for me.. :(

    Can you please tell me, how did u achieve this

    My e-mail : yogesh249@gmail.com
  • Devlshone
    Can $.get return an array?
  • me
    somescrit.php:
    echo('garbage');
    ?>

    index.php:
    <div id="code"></div>
    $('#code').load('http://localhost/somescript.php'); this isn't work in 'security shit context' i don't understand why



  • Fernando
    why dont u try:
    $(document).ready(
    function () {
    $('#code').load('http://localhost/somescript.php');
    }
    );
  • Lalit Patel
    Hi I am using php.
    I have 2 div with unique id and i get content on it with Jquery.get sometimes 1st div data comes in 2nd div and 2nd div data in 1st div.

    What is solution of this problem. Please answer me.