MOCKSTACKS
EN
Questions And Answers

More Tutorials









Jquery All in one examples

Ajax Get:


Solution 1:

$.get('url.html', function(data){
 $('#update-box').html(data);
});

Solution 2:

$.ajax({
 type: 'GET',
 url: 'url.php', 
}).done(function(data){
 $('#update-box').html(data);
}).fail(function(jqXHR, textStatus){
 alert('Error occurred: ' + textStatus);
});


Ajax Load: Another ajax get method created for simplcity.

$('#update-box').load('url.html');

.load can also be called with additional data. The data part can be provided as string or object.

$('#update-box').load('url.php', {data: "something"});
$('#update-box').load('url.php', "data=something");

If .load is called with a callback method, the request to the server will be a post

$('#update-box').load('url.php', {data: "something"}, function(resolve){
 //do something
});

Ajax Post:


Solution 1:

$.post('url.php',
 {date1Name: data1Value, date2Name: data2Value}, //data to be posted
 function(data){
 $('#update-box').html(data);
 }
);

Solution 2:

$.ajax({
 type: 'Post',
 url: 'url.php', 
 data: {date1Name: data1Value, date2Name: data2Value} //data to be posted
}).done(function(data){
 $('#update-box').html(data);
}).fail(function(jqXHR, textStatus){
 alert('Error occurred: ' + textStatus);
});

Ajax Post JSON:

var postData = {
 Name: name,
 Address: address,
 Phone: phone
};
$.ajax({
 type: "POST",
 url: "url.php",
 dataType: "json",
 data: JSON.stringfy(postData),
 success: function (data) {
 //here variable data is in JSON format
 }
});

Ajax Get JSON:


Solution 1:

$.getJSON('url.php', function(data){
 //here variable data is in JSON format
});

Solution 2:

$.ajax({
 type: "Get",
 url: "url.php",
 dataType: "json",
 data: JSON.stringfy(postData),
 success: function (data) {
 //here variable data is in JSON format
 }, 
 error: function(jqXHR, textStatus){
 alert('Error occurred: ' + textStatus);
 }
 });



Conclusion

In this page (written and validated by ) you learned about Jquery All in one examples . What's Next? If you are interested in completing Jquery tutorial, your next topic will be learning about: Jquery Ajax File Uploads.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.