1

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.

  • $('#result').load('ajax/test.html'); //ajax load a html file , it use Get method.
  • $('#result').load('ajax/test.html .someclass'); //ajax load the selected content from a html file.
  • $('#result').load('ajax/test.html', function() { alert('Load was performed.');});//call back when it succeed.
  • $(document).ready(function(){ $("input").keyup(function(){ txt=$("input").val(); $("span").load("/jquery/gethint.asp",{suggest:txt}); }); });//I am not sure what is this data mean. How the server side get this data?

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.

2
  • Data is string. What do you want? Commented Mar 3, 2013 at 14:48
  • Data could be string / array / object, I am not sure how to get the data from the server side .thanks. Commented Mar 3, 2013 at 14:51

2 Answers 2

2

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
%>
Sign up to request clarification or add additional context in comments.

6 Comments

@Joe.wang That's what I said. I haven't tried it, so I should not comment! :P
you answer is very good.but if you can show us the server side code , it will be appreciated. thanks.Anyway,
What kind of server side code do you want? I don't get you. Anyways, I will give for ASP and well as PHP. Give some time.
@Joe.wang Kindly confirm the ASP Code. Its been years using it.
I almost forgot the asp. :P ,It works well. It is very kind of you . thanks!
|
1

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.

2 Comments

Just edited to make some code formatting, hope you don't mind. :)
@Rodik , It is very helpful link. I can't vote it up for more times. I think load is one of the helpful api in them. thanks you very much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.