JQuery 操作 AJAX

JQuery 操作 AJAX

Table of Contents

  • .ajax
  • .load – get
  • .load – post
  • .load – 回调函数
  • .load 解析 json
  • .load 解析 xml
  • .get 解析 json
  • .post 和 xml
  • .post 和 json
  • 相关阅读
$.ajax
$(DOM).load   包含 get 和 post 方式, 请求的数据会放在 DOM 里面
$.post
$.get

.ajax

// json
$.ajax({
    type: "POST",
    url: "server.php",
    data: { name: xxx, number: xxx },
    dataType: "json",
    success: function(data){
        alert(data.msg);
    },
    error: function(jqXHR){
        alert("发生错误:" + jqXHR.status);
    },
});

// xml
$.ajax({
    type: ‘GET‘,
    url: ‘http://localhost/test.php‘,
    dataType: ‘xml‘,
    success: function (data) {
        alert(data.getElementsByTagName(‘url‘)[0].childNodes[0].nodeValue);
    },
    error: function (xhr) {
        alert(xhr.statusText);
    }
});

.load – get

<input type="button" value="load data">
<div id="box"></div>

$(‘input‘).click(function () {
    $(‘#box‘).load(‘test.html‘);
});

// test.html 的内容是
// <span>test</span>
// <class>test</class>

$(‘input‘).click(function () {
    $(‘#box‘).load(‘test.html .my‘);        // 选择 class 为 my 的数据
});

load 请求的如果是 .html 静态文件, 那么直接返回静态文件里面的内容, 并且可以对其做
筛选, 如果 load 的 url 是 php 动态文件, 那么会返回动态文件生成的内容, 例如 echo

test.php

if ($_GET[‘url‘] == ‘test‘) {
    echo ‘you come here‘;
}
$(‘input‘).click(function () {
    $(‘#box‘).load(‘test.php?url=test‘);   // div box 里面的内容会变成 you come here
});

.load – post

test2.php

if ($_POST[‘url‘] == ‘test‘) {
    echo ‘you come here‘;
}
$("input").click(function() {
    var data = { url: ‘test‘ };
    $("#box").load("test.php", data);
});

.load – 回调函数

$(‘input‘).click(function () {
    var data = { url : ‘test‘ };
    $(‘#box‘).load(‘test.php‘, data, function (response, status, xhr) {
        alert(‘返回的值为:‘ + response + ‘, 状态为:‘ + status + ‘, 状态是:‘ + xhr.statusText);
        // status: success
    });
});

.load 解析 json

test.php

<?php

echo ‘{ "url": "www.example.com" }‘;
$(‘input‘).click(function () {
    $(‘#box‘).load(‘test.php‘, null, function (res, status, xhr) {
         var data = JSON.parse(res);
         alert(data.url);
    });
});

.load 解析 xml

test.php

<?php

header(‘content-type: text/xml‘);
echo ‘<root><url>www.example.com</url></root>‘;

test.js

$(‘input‘).click(function () {
    $(‘#box‘).load(‘test.php‘, null, function (res, status, xhr) {
        var xml = res;
        alert($(xml).find(‘url‘).html());
    });
});

.get 解析 json

<?php

echo ‘{"root": {"url": "www.example.com"}}‘;
$.get("test.php", {
        url: ‘test‘
    }, function(response, status, xhr){
    var res = JSON.parse(response);
    alert(res.root.url);
});

.post 和 xml

test.php

<?php

header(‘content-type: text/xml‘);
echo ‘<root><url>www.example.com</url></root>‘;
$("input").click(function(){
    $.post("test.php", function(response, status, xhr){
        alert($(response).find(‘root‘).find(‘url‘).text());
        alert($(response).find(‘url‘).text());
    });
});

.post 和 json

test.php

<?php
echo ‘{ "url": "www.example.com" }‘;
$("input").click(function(){
    $.post("test.php", function(response, status, xhr){
        alert(response.url);
    });
});

test.php

echo ‘[{ "url": "www.example1.com" }, { "url": "www.example2.com" }]‘;
$("input").click(function(){
    $.post("test.php", function(response, status, xhr){
        var res = JSON.parse(response);
        alert(res[1].url);
    });
});

相关阅读

时间: 2024-08-15 11:01:43

JQuery 操作 AJAX的相关文章

jquery操作Ajax返回的JSON值

$.post( 'XXX.php', { col: value }, function(data) { var getData = eval( "(" + data + ")" ); //必须用括号,真蛋疼 alert( getData.xx ); } ); jquery操作Ajax返回的JSON值,布布扣,bubuko.com

jquery操作ajax返回的页面元素

这两天工作不忙,正好从朋友那里拿到一个某个应用的开发文档,相关数据放在了mongodb里,自己电脑可以本地开启服务器然后通过给的借口来获取数据.由于这是一个比较大比较全的一个完整项目,也没有那么多经历全部做一下,就找了其中几部分来做一下,由于是一个电商类的移动端,所以那些数据都是动态加载上去的,通过ajax动态获取然后添加到页面上,所以主要是模板引擎的使用和ajax的运用,其他就是一些样式的操作.虽然以前也做过,但是这次碰到了一个问题,就是需要在ajax返回的内容里找到某个元素,然后给那个元素绑

JQuery操作Ajax

今天就来说说jquery对于ajax的支持,jquery封装了XMLHttpRequest的底层实现,直接调用提供的方法即可 1.$.ajax(options) 这个方法是jquery对于ajax最为全面的支持,$.ajax(options)既可以发送GET请求,也可以发送POST请求等等,因此我们通过这个方法可以获得ajax交互的所有控制权. 该方法中包含了一个参数options,该参数的形式为{key1:val1,key2:val2,key3:val3....},如 { url:delTag

jquery 操作ajax 相关方法

jQuery.get() 使用一个HTTP GET 请求从服务器加载数据. jQuery.get(url [,data] [,success(data,textStatus,jqXHR)] [dtaType]) url 一个包含发送请求的URL data 发送给服务器的字符串后键值对 success() 当请求成功时回调的函数 dataType 从服务器返回的预期数据. 用法: $.get("test.cgi", { name: "John", time: &quo

JS 操作 AJAX

JS 操作 AJAX Table of Contents API 同步和异步 ajax / text server get post ajax / json server get post ajax / xml server get post 跨域 相关阅读 API onreadystatechange 指定当 readyState 属性改变时的事件处理句柄 readyState 返回当前请求的状态 responseBody 将回应信息正文以 unsigned byte 数组形式返回 respo

jQuery操作dom事件

参考:jQuery权威指南jQuery初步jQuery选择器jQuery操作domjQuery操作dom事件jQuery插件jQuery操作AjaxjQuery动画与特效jQuery实现导航栏jQuery实现点击式选项卡jQuery实现select三级联动 //1.绑定事件bind() bind('event name', eventData, function(event) { /* Act on the event */ }); //event name事件名称:可接收事件列表 blur,f

【JAVAWEB学习笔记】28_jqueryAjax:json数据结构、jquery的ajax操作和表单校验插件

Ajax-jqueryAjax 今天内容: 1.json数据结构(重点) 2.jquery的ajax操作(重点) 3.jquery的插件使用   一.json数据结构 1.什么是json JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于ECMAScript的一个子集. JSON采用完全独立于语言的文本格式,但是也使用了类似于C语言家族的习惯.这些特性使JSON成 为理想的数据交换语言.易于人阅读和编写,同时也易于机器解析和生成(一般用于提升网络

JavaScript之Ajax-6 Ajax的增强操作(jQuery对Ajax的支持、表单操作)

一.jQuery对Ajax的支持 load() - 作用: 将服务器返回的数据字节添加到符合要求的节点之上 - 用法: $obj.load(请求地址,请求参数) - 请求参数 - "username=tom&age=22" - {'username':'tom','age':22} - 有请求参数时,load方法发送POST请求,否则发送GET请求 get() - 作用: 发送GET类型的请求 - 用法: $.get(请求地址,请求参数,回调函数,服务器返回的数据类型) - 说

JQuery中Ajax的操作

Java软件开发中,后台中我们可以通过各种框架,像SSH等进行对代码的封装,方便我们对Java代码的编写,例如,Struts,SpringMVC对从前台到action的流程进行封装控制,使我们只需要进行一些简单配置就可以实现:而Spring进行了对各种对象的管理进行封装,提供了AOP编程的方式,大大方便了我们:而Hibernate和IBatis则是对JDBC代码进行封装,不需要我们每次都写那些重复而繁杂的JDBC代码. 前台呢,对于页面一些效果,验证等,我们都是通过JavaScript语言进行完