Joe.wang
Joe.wang

Reputation: 11793

Jquery Ajax load method

All , forgive me I am a newbie of jquery .I found there doesn't exist more detail of this api in jquery load api. Here is what I had lean from it and I have some the question about it.please review it.

This api include these ways of usage to make a ajax call.

So far , I didn't find the example when data is a string which will add in the url as parameters by jquery. I hope someone also can illustrate some code for me . thanks.

Updated

Please note the loadapi always be with the serialize method to format the UI input values to json. thanks.

Upvotes: 1

Views: 623

Answers (2)

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167172

The data in jQuery $.load() can be given in these ways.

  • As a JSON object.

      data: {"foo": "bar"}
    
  • As a string

      data: "foo=bar"
    

You can use both the ways. The first one is the object way.


For your four queries:

  • Yes, you are right. It uses GET method.
  • From a HTML file, and with that particular class's HTML. Not sure if classes are accepted, but I have used ID.
  • Yes, you are right. Executes some JavaScript after the AJAX Request is done.
  • This data is explained above.

Server Side Script

PHP

<?php
  if (!isset($_GET['foo']) && $_GET['foo'] == "bar")
    die("true");
  else
    die("false");
?>

ASP

<%
  IF Request.Form("foo") = "bar" Then
    Response.Write "true"
  ELSE
    Response.Write "false"
  END IF
%>

Upvotes: 2

Rodik
Rodik

Reputation: 4092

there are different ways to use Ajax in jQuery, with the basic one being .ajax().

Here are some useful shorthand methods: http://api.jquery.com/category/ajax/shorthand-methods/

These methods do the same as ajax, only with easier syntax and preconfigured to do specific tasks.

I'm not sure i understood your question, but i'll answer for any interpertation of your question:

  1. if you want to send a parameter to the server, you use the second parameter of the load function to send a parameter, as such:

    $('#result').load('ajax/test.html', {prop:val, prop2:val});
    

    these props will be added to your request (either by get or by post methods, as per configuration)

  2. if you wish to recieve the data from the server into a parameter, you use the following:

    $('#result').load('ajax/test.html', function(data){ console.log(data); });
    

    in this case, function is a callback that runs once the request is complete, and data holds all of the contents of the response from the server.

Upvotes: 1

Related Questions