javascript AJAX片段
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript AJAX片段相关的知识,希望对你有一定的参考价值。
<script type='text/javascript'>
$(document).ready(function() {
$(function() {
var href;
var title;
$('body').on('click','a.ajax-link',function(e) { // nav link clicked
// get link href
href = $(this).attr("href");
// get input name
title = $(this).attr("name");
// load content via AJAX
loadContent(href);
// prevent click and reload
e.preventDefault();
$(".spinner").removeClass('uk-hidden');
});
function loadContent(url){ // Load content
// variable for page data
$pageData = '';
// send Ajax request
$.ajax({
type: "POST",
url: url,
data: { ajax: true },
success: function(data,status){
$pageData = data;
}
}).done(function(){ // when finished and successful
// remove old content
$('.ajax-current-content').remove();
// construct new content
$pageData = '<div class="ajax-current-content">' + $pageData + '</div>';
// add content to page
$('#ajax-container').append($pageData);
$(".spinner").addClass('uk-hidden');
}); // end of ajax().done()
} // end of loadContent()
});
});
</script>
<!-- links to call ajax content -->
<a class="ajax-link" href="<?=$pages->get('/search/')->url?>">Load Seach</a>
<a class="ajax-link" href="<?=$pages->get('/')->url?>">Load Home</a>
<!-- uikit spinner icons -->
<div class="spinner uk-hidden" uk-spinner></div>
<!-- replace ajax-current-content with new -->
<div id="ajax-container">
<div class="ajax-current-content">
<h3>This content will be replacer</h3>
</div>
</div>
$.ajax({
url: 'http://maps.googleapis.com/maps/api/geocode/json?address='+ location +'&sensor=false',
success: function(data) {
console.log(data);
}
});
/**
* Getting Data with Fetch
*/
fetch('https://api.github.com/users/lokomotivan/repos')
.then(response => response.json()) // (XML -> response.text() | image -> response.blob)
.then(data => {
// Here's a list of repos!
console.log(data)
});
/**
* Sending data with Fetch
*/
let content = {some: 'content'};
// The actual fetch request
fetch('some-url', {
method: 'post', // can be: post, put, del
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(content)
})
// .then()...
/**
* Fetch Error Handling
*/
fetch('https://api.github.com/users/chrissycoyier/repos')
.then(response => {
if (response.ok) {
return response.json()
} else {
return Promise.reject('something went wrong!')
}
})
.then(data => console.log('data is', data))
.catch(error => console.log('error is', error));
/**
* Fetch Error Handling FULL
*/
fetch('https://api.github.com/users/chrissycoyier/repos')
.then(response => {
if (response.ok) {
return response.json()
} else {
return Promise.reject({
status: response.status,
statusText: response.statusText
})
}
})
.catch(error => {
if (error.status === 404) {
// do something about 404
}
})
以上是关于javascript AJAX片段的主要内容,如果未能解决你的问题,请参考以下文章
html PHP代码片段: - AJAX基本示例:此代码演示了使用PHP和JavaScript实现的基本AJAX功能。
Javascript代码片段在drupal中不起作用
AJAX 响应:数据(JSON、XML)还是 HTML 片段? [关闭]
JavaScript-Tool-lhgDialog:动画示例
Thymeleaf - 如何交互和重新加载 Javascript?
Googlebot是否真的不等待AJAX 请求完成?