當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
034_jQuery Ajax的getJSON和getScript方法
生活随笔
收集整理的這篇文章主要介紹了
034_jQuery Ajax的getJSON和getScript方法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1. $.getJSON()方法
1.1. 通過HTTP GET請求載入JSON數據。
1.2. 語法
$.getJSON(url,data,function(response,textStatus,jqXHR))1.3. 參數
1.4. 該函數是簡寫的Ajax函數, 等價于:
$.ajax({url: url,data: data,success: function(response,textStatus,jqXHR){},dataType: json });2. getScript()方法
2.1. getScript()方法通過HTTP GET請求載入并執行JavaScript文件。
2.2. 語法
$.getScript(url, function(response,textStatus,jqXHR))2.3. 參數
2.4. 該函數是簡寫的Ajax函數, 等價于:
$.ajax({url: url,dataType: "script",success: function(response,textStatus,jqXHR){} });3. 例子
3.1. 新建一個名叫jQueryGetJSONScript的動態WEB工程
3.2. 新建index.html
<!DOCTYPE html> <html><head><title>jQuery-Ajax的getJSON()和getScript()方法</title><meta charset="utf-8" /><script type="text/javascript" src="jquery.js"></script><script type="text/javascript">$(document).ready(function(){$('#btn1').click(function(){$.getJSON('test.json','r='+Math.random()+'&time='+new Date().getTime(),function(response,textStatus,jqXHR){$("#result").text('code='+response.data.code+', info='+response.data.info+', msg='+response.data.msg);});});$('#btn2').click(function(){$.getScript('test.js',function(response,textStatus,jqXHR){$(this).html(response);});});});</script><style type="text/css">div {width: 450px;height: 100px;background-color: pink;}</style></head><body> <div id="result">結果區域</div><br /><button id="btn1">getJSON</button> <button id="btn2">getScript</button></body> </html>3.3. 新建test.json
{"data": {"code": 1, "info": "success", "msg": "請求成功。"}}3.4. 新建test.js
document.getElementById('result').innerHTML='我是服務器使用js返回的文本。';3.5. 運行項目
總結
以上是生活随笔為你收集整理的034_jQuery Ajax的getJSON和getScript方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 033_jQuery Ajax的ajax
- 下一篇: 035_jQuery Ajax的ajax