国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Table of Contents
Example
Definition and Usage
Syntax
詳細說明
示例
例子 3
定義和用法
語法
Home Web Front-end JS Tutorial jQuery ajax - getScript() method and getJSON method

jQuery ajax - getScript() method and getJSON method

Jul 03, 2017 am 10:25 AM
javascript



Example

Use AJAX request to obtain JSON data and output the result:

$("button").click(function(){
??$.getJSON("demo_ajax_json.js",function(result){
????$.each(result,?function(i,?field){
??????$("p").append(field?+?"?");
????});
??});
});



Definition and Usage

Load JSON data via HTTP GET request.

In jQuery 1.2, you can load JSON data from other domains by using a callback function in JSONP form, such as "myurl?callback=?". jQuery will automatically replace ? with the correct function name to execute the callback function. Note: The code after this line will be executed before this callback function is executed.

Syntax

jQuery.getJSON(url,data,success(data,status,xhr))
Parameters Description
url Required. Specifies the URL to which the request will be sent.
data Optional. Specifies the data to be sent to the server with the request.
success(data,status,xhr)

Optional. Specifies a function to run when the request is successful.

Extra parameters:

  • ##response - Contains the result data from the request

  • status - Contains the status of the request

  • xhr - Contains XMLHttpRequest Object

詳細說明

該函數(shù)是簡寫的 Ajax 函數(shù),等價于:

$.ajax({
??url:?url,
??data:?data,
??success:?callback,
??dataType:?json
});

發(fā)送到服務器的數(shù)據(jù)可作為查詢字符串附加到 URL 之后。如果?data?參數(shù)的值是對象(映射),那么在附加到 URL 之前將轉(zhuǎn)換為字符串,并進行 URL 編碼。

傳遞給?callback?的返回數(shù)據(jù),可以是 JavaScript 對象,或以 JSON 結(jié)構(gòu)定義的數(shù)組,并使用 $.parseJSON() 方法進行解析。

示例

從 test.js 載入 JSON 數(shù)據(jù)并顯示 JSON 數(shù)據(jù)中一個 name 字段數(shù)據(jù):

$.getJSON("test.js",?function(json){
??alert("JSON?Data:?"?+?json.users[3].name);
});


例子 1

從 Flickr JSONP API 載入 4 張最新的關(guān)于貓的圖片:

HTML 代碼:

<p id="images"></p>

jQuery 代碼:

$.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?
tags=cat&tagmode=any&format=json&jsoncallback=?",?function(data){
??$.each(data.items,?function(i,item){
????$("<img/>").attr("src",?item.media.m).appendTo("#images");
????if?(?i?==?3?)?return?false;
??});
});

例子 2

從 test.js 載入 JSON 數(shù)據(jù),附加參數(shù),顯示 JSON 數(shù)據(jù)中一個 name 字段數(shù)據(jù):

$.getJSON("test.js",?{?name:?"John",?time:?"2pm"?},?function(json){
??alert("JSON?Data:?"?+?json.users[3].name);
});
<h1 style="margin:15px 0px 0px 15px; padding:0px; border:0px; font-family:微軟雅黑; font-size:26px; font-weight:400; background-color:rgb(249,249,249)">jQuery ajax - getScript() 方法</h1><p style="margin:0px 0px 0px 15px; padding:20px 0px; border-width:1px 0px; border-top-style:solid; border-bottom-style:solid; border-top-color:rgb(170,170,170); border-bottom-color:rgb(170,170,170); width:710px; font-family:Verdana,Arial,宋體; background-color:rgb(249,249,249)"><br></p><h2 style="margin:0px; padding:0px; border:0px; font-family:微軟雅黑; font-size:14px">實例</h2><p style="margin-top:12px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; border:0px; line-height:18px">通過 AJAX 請求來獲得并運行一個 JavaScript 文件:</p><pre style="margin-top:10px; margin-bottom:0px; padding:10px; border:1px dotted rgb(119,136,85); font-family:'Courier New',Courier,monospace; width:620px; background-color:rgb(245,245,245)">$("button").click(function(){
??$.getScript("demo_ajax_script.js");
});


定義和用法

getScript() 方法通過 HTTP GET 請求載入并執(zhí)行 JavaScript 文件。

語法

jQuery.getScript(url,success(response,status))
參數(shù) 描述
url 將要請求的 URL 字符串。
success(response,status)

可選。規(guī)定請求成功后執(zhí)行的回調(diào)函數(shù)。

額外的參數(shù):

  • response?- 包含來自請求的結(jié)果數(shù)據(jù)

  • status?- 包含請求的狀態(tài)("success", "notmodified", "error", "timeout" 或 "parsererror")

詳細說明

該函數(shù)是簡寫的 Ajax 函數(shù),等價于:

$.ajax({
??url:?url,
??dataType:?"script",
??success:?success});

這里的回調(diào)函數(shù)會傳入返回的 JavaScript 文件。這通常不怎么有用,因為那時腳本已經(jīng)運行了。

載入的腳本在全局環(huán)境中執(zhí)行,因此能夠引用其他變量,并使用 jQuery 函數(shù)。

比如加載一個 test.js 文件,里邊包含下面這段代碼:

$(".result").html("<p>Lorem?ipsum?dolor?sit?amet.</p>");

通過引用該文件名,就可以載入并運行這段腳本:

$.getScript("ajax/test.js",?function()?{
??alert("Load?was?performed.");
});

注釋jQuery 1.2 版本之前,getScript 只能調(diào)用同域 JS 文件。 1.2中,您可以跨域調(diào)用 JavaScript 文件。注意:Safari 2 或更早的版本不能在全局作用域中同步執(zhí)行腳本。如果通過 getScript 加入腳本,請加入延時函數(shù)。


例子 1

加載并執(zhí)行 test.js:

$.getScript("test.js");

例子 2

加載并執(zhí)行 test.js ,成功后顯示信息:

$.getScript("test.js",?function(){
??alert("Script?loaded?and?executed.");
});

例子 3

載入?jQuery 官方顏色動畫插件?成功后綁定顏色變化動畫:

HTML 代碼:

<button id="go">Run</button>
<p class="block"></p>

jQuery 代碼:

jQuery.getScript("http://dev.jquery.com/view/trunk/plugins/color/jquery.color.js",
?function(){
??$("#go").click(function(){
????$(".block").animate(?{?backgroundColor:?'pink'?},?1000)
??????.animate(?{?backgroundColor:?'blue'?},?1000);
??});
});

The above is the detailed content of jQuery ajax - getScript() method and getJSON method. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

How to implement an online reservation system using WebSocket and JavaScript How to implement an online reservation system using WebSocket and JavaScript Dec 17, 2023 am 09:39 AM

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

How to use insertBefore in javascript How to use insertBefore in javascript Nov 24, 2023 am 11:56 AM

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

How to get HTTP status code in JavaScript the easy way How to get HTTP status code in JavaScript the easy way Jan 05, 2024 pm 01:37 PM

Introduction to the method of obtaining HTTP status code in JavaScript: In front-end development, we often need to deal with the interaction with the back-end interface, and HTTP status code is a very important part of it. Understanding and obtaining HTTP status codes helps us better handle the data returned by the interface. This article will introduce how to use JavaScript to obtain HTTP status codes and provide specific code examples. 1. What is HTTP status code? HTTP status code means that when the browser initiates a request to the server, the service

See all articles