1. 程式人生 > 實用技巧 >Ajax函式的封裝

Ajax函式的封裝

Ajax函式的封裝

<script type="text/javascript">
			function ajax(options) {
					// 1 建立Ajax物件
					let xhr = new XMLHttpRequest();
					// 2 告訴Ajax物件要想哪兒傳送請求,以什麼方式傳送請求
					// 1)請求方式2)請求地址
					xhr.open('get', 'http://localhost:3003/basicUseOfAjaxTest');
					// 傳送請求
					xhr.send(options.type, options.url);
					// 獲取伺服器端響應到客戶端的資料!
					xhr.onload = function() {
						console.log(xhr.responseText)
					}
	
			}
			ajax({
				type: 'get',
				url: 'http://localhost:3003/basicUseOfAjaxTest'
			})
	</script>

部落格園是面向開發者的知識分享社群,不允許釋出任何推廣、廣告、政治方面的內容。
部落格園首頁(即網站首頁)只能釋出原創的、高質量的、能讓讀者從中學到東西的內容。
如果博文質量不符合首頁要求,會被工作人員移出首頁,望理解。如有疑問,請聯絡[email protected]