js和ajax都是我们程序中必不可少的,它们之间也有紧密的联系。本文我们主要和大家分享js实现的ajax和同源策略的实例讲解,具有很好的参考价值,希望能帮助到大家。
一、回顾jquery实现的ajax
首先说一下ajax的优缺点
优点:
ajax使用javascript技术向服务器发送异步请求;
ajax无须刷新整个页面;
因为服务器响应内容不再是整个页面,而是页面中的局部,所以ajax性能高;
jquery 实现的ajax
index.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>title</title>
<script src="{% static 'js/jquery-3.1.1.js' %}"></script>
</head>
<body>
<button class="send_ajax">send_ajax</button>
<script>
//$.ajax的两种使用方式:
//$.ajax(settings);
//$.ajax(url,[settings]);
$(".send_ajax").click(function(){
$.ajax({
url:"/handle_ajax/",
type:"post",
data:{username:"yuan",password:123},
success:function(data){
alert(data)
},
//=================== error============
error: function (jqxhr, textstatus, err) {
// jqxhr: jquery增强的xhr
// textstatus: 请求完成状态
// err: 底层通过throw抛出的异常对象,值与错误类型有关
console.log(arguments);
},
//=================== complete============
complete: function (jqxhr, textstatus) {
// jqxhr: jquery增强的xhr
// textstatus: 请求完成状态 success | error
console.log('statuscode: %d, statustext: %s', jqxhr.status, jqxhr.statustext);
console.log('textstatus: %s', textstatus);
},
//=================== statuscode============
statuscode: {
'403': function (jqxhr, textstatus, err) {
console.log(arguments); //注意:后端模拟errror方式:httpresponse.status_code=500
},
'400': function () {
}
}
})
})
</script>
</body>
</html>
views.py
import json,time
def index(request):
return render(request,"index.html")
def handle_ajax(request):
username=request.post.get("username")
password=request.post.get("password")
print(username,password)
time.sleep(10)
return httpresponse(json.dumps("error data!"))
$.ajax参数
请求参数
######################------------data---------################
data: 当前ajax请求要携带的数据,是一个json的object对象,ajax方法就会默认地把它编码成某种格式
(urlencoded:?a=1&b=2)发送给服务端;此外,ajax默认以get方式发送请求。
function testdata() {
$.ajax("/test",{ //此时的data是一个json形式的对象
data:{
a:1,
b:2
}
}); //?a=1&b=2
######################------------processdata---------################
processdata:声明当前的data数据是否进行转码或预处理,默认为true,即预处理;if为false,
那么对data:{a:1,b:2}会调用json对象的tostring()方法,即{a:1,b:2}.tostring()
,最后得到一个[object,object]形式的结果。
######################------------contenttype---------################
contenttype:默认值: "application/x-www-form-urlencoded"。发送信息至服务器时内容编码类型。
用来指明当前请求的数据编码格式;urlencoded:?a=1&b=2;如果想以其他方式提交数据,
比如contenttype:"application/json",即向服务器发送一个json字符串:
$.ajax("/ajax_get",{
data:json.stringify({
a:22,
b:33
}),
contenttype:"application/json",
type:"post",
}); //{a: 22, b: 33}
注意:contenttype:"application/json"一旦设定,data必须是json字符串,不能是json对象
views.py: json.loads(request.body.decode("utf8"))
######################------------traditional---------################
traditional:一般是我们的data数据有数组时会用到 :data:{a:22,b:33,c:["x","y"]},
traditional为false会对数据进行深层次迭代;
响应参数
/*
datatype: 预期服务器返回的数据类型,服务器端返回的数据会根据这个值解析后,传递给回调函数。
默认不需要显性指定这个属性,ajax会根据服务器返回的content type来进行转换;
比如我们的服务器响应的content type为json格式,这时ajax方法就会对响应的内容
进行一个json格式的转换,if转换成功,我们在success的回调函数里就会得到一个json格式
的对象;转换失败就会触发error这个回调函数。如果我们明确地指定目标类型,就可以使用
data type。
datatype的可用值:html|xml|json|text|script
见下datatype实例
*/
小练习:计算两个数的和
方式一:这里没有指定contenttype:默认是urlencode的方式发的
index.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<meta name="viewport" content="width=device-width">
<title>title</title>
<script src="/static/jquery-3.2.1.min.js"></script>
<script src="http://apps.bdimg.com/libs/jquery.cookie/1.4.1/jquery.cookie.js"></script>
</head>
<body>
<h1>计算两个数的和,测试ajax</h1>
<input type="text" class="num1">+<input type="text" class="num2">=<input type="text" class="ret">
<button class="send_ajax">sendajax</button>
<script>
$(".send_ajax").click(function () {
$.ajax({
url:"/sendajax/",
type:"post",
headers:{"x-csrftoken":$.cookie('csrftoken')},
data:{
num1:$(".num1").val(),
num2:$(".num2").val(),
},
success:function (data) {
alert(data);
$(".ret").val(data)
}
})
})
</script>
</body>
</html>
views.py
def index(request):
return render(request,"index.html")
def sendajax(request):
print(request.post)
print(request.get)
print(request.body)
num1 = request.post.get("num1")
num2 = request.post.get("num2")
ret = float(num1)+float(num2)
return httpresponse(ret)
方式二:指定conenttype为json数据发送:
index2.html
<script>
$(".send_ajax").click(function () {
$.ajax({
url:"/sendajax/",
type:"post",
headers:{"x-csrftoken":$.cookie('csrftoken')}, //如果是json发送的时候要用headers方式解决forbidden的问题
data:json.stringify({
num1:$(".num1").val(),
num2:$(".num2").val()
}),
contenttype:"application/json", //客户端告诉浏览器是以json的格式发的数据,所以的吧发送的数据转成json字符串的形式发送
success:function (data) {
alert(data);
$(".ret").val(data)
}
})
})
</script>
views.py
def sendajax(request):
import json
print(request.post) #<querydict: {}>
print(request.get) #<querydict: {}>
print(request.body) #b'{"num1":"2","num2":"2"}' 注意这时的数据不再post和get里,而在body中
print(type(request.body.decode("utf8"))) # <class 'str'>
# 所以取值的时候得去body中取值,首先得反序列化一下
data = request.body.decode("utf8")
data = json.loads(data)
num1= data.get("num1")
num2 =data.get("num2")
ret = float(num1)+float(num2)
return httpresponse(ret)
二、js实现的ajax
1、ajax核心(xmlhttprequest)
其实ajax就是在javascript中多添加了一个对象:xmlhttprequest对象。所有的异步交互都是使用xmlhttpservlet对象完成的。也就是说,我们只需要学习一个javascript的新对象即可。
var xmlhttp = new xmlhttprequest();(大多数浏览器都支持dom2规范)
注意,各个浏览器对xmlhttprequest的支持也是不同的!为了处理浏览器兼容问题,给出下面方法来创建xmlhttprequest对象:
function createxmlhttprequest() {
var xmlhttp;
// 适用于大多数浏览器,以及ie7和ie更高版本
try{
xmlhttp = new xmlhttprequest();
} catch (e) {
// 适用于ie6
try {
xmlhttp = new activexobject("msxml2.xmlhttp");
} catch (e) {
// 适用于ie5.5,以及ie更早版本
try{
xmlhttp = new activexobject("microsoft.xmlhttp");
} catch (e){}
}
}
return xmlhttp;
}
2、使用流程
1、打开与服务器的连接(open)
当得到xmlhttprequest对象后,就可以调用该对象的open()方法打开与服务器的连接了。open()方法的参数如下:
open(method, url, async):
method:请求方式,通常为get或post;
url:请求的服务器地址,例如:/ajaxdemo1/aservlet,若为get请求,还可以在url后追加参数;
async:这个参数可以不给,默认值为true,表示异步请求;
var xmlhttp = createxmlhttprequest();
xmlhttp.open("get", "/ajax_get/?a=1", true);
2、发送请求
当使用open打开连接后,就可以调用xmlhttprequest对象的send()方法发送请求了。send()方法的参数为post请求参数,即对应http协议的请求体内容,若是get请求,需要在url后连接参数。
注意:若没有参数,需要给出null为参数!若不给出null为参数,可能会导致firefox浏览器不能正常发送请求!
xmlhttp.send(null);
3、接收服务器的响应(5个状态,4个过程)
当请求发送出去后,服务器端就开始执行了,但服务器端的响应还没有接收到。接下来我们来接收服务器的响应。
xmlhttprequest对象有一个onreadystatechange事件,它会在xmlhttprequest对象的状态发生变化时被调用。下面介绍一下xmlhttprequest对象的5种状态:
0:初始化未完成状态,只是创建了xmlhttprequest对象,还未调用open()方法;
1:请求已开始,open()方法已调用,但还没调用send()方法;
2:请求发送完成状态,send()方法已调用;
3:开始读取服务器响应;
4:读取服务器响应结束。
onreadystatechange事件会在状态为1、2、3、4时引发。
下面代码会被执行四次!对应xmlhttprequest的四种状态!
xmlhttp.onreadystatechange = function() {
alert('hello');
};
但通常我们只关心最后一种状态,即读取服务器响应结束时,客户端才会做出改变。我们可以通过xmlhttprequest对象的readystate属性来得到xmlhttprequest对象的状态。
xmlhttp.onreadystatechange = function() {
if(xmlhttp.readystate == 4) {
alert('hello');
}
};
其实我们还要关心服务器响应的状态码xmlhttp.status是否为200,其服务器响应为404,或500,那么就表示请求失败了。我们可以通过xmlhttprequest对象的status属性得到服务器的状态码。
最后,我们还需要获取到服务器响应的内容,可以通过xmlhttprequest对象的responsetext得到服务器响应内容。
xmlhttp.onreadystatechange = function() {
if(xmlhttp.readystate == 4 && xmlhttp.status == 200) {
alert(xmlhttp.responsetext);
}
};
需要注意的:
如果是post请求:
基于js的ajax没有content-type这个参数了,也就不会默认是urlencode这种形式了,需要我们自己去设置
<1>需要设置请求头:xmlhttp.setrequestheader(“content-type”, “application/x-www-form-urlencoded”);
注意 :form表单会默认这个键值对不设定,web服务器会忽略请求体的内容。
<2>在发送时可以指定请求体了:xmlhttp.send(“username=yuan&password=123”)
基于jquery的ajax和form发送的请求,都会默认有content-type,默认urlencode,
content-type:客户端告诉服务端我这次发送的数据是什么形式的
datatype:客户端期望服务端给我返回我设定的格式
如果是get请求:
xmlhttp.open("get","/sendajax/?a=1&b=2");
小练习:和上面的练习一样,只是换了一种方式(可以和jquery的对比一下)
<script>
方式一=======================================
//基于js实现实现用urlencode的方式
var ele_btn = document.getelementsbyclassname("send_ajax")[0];
ele_btn.onclick = function () { //绑定事件
alert(5555);
//发送ajax:有一下几步
//(1)获取xmlresquest对象
xmlhttp = new xmlhttprequest();
//(2)连接服务器
//get请求的时候,必用send发数据,直接在请求路径里面发
{# xmlhttp.open("get","/sendajax/?a=1&b=2");//open里面的参数,请求方式,请求路径#}
//post请求的时候,需要用send发送数据
xmlhttp.open("post","/sendajax/");
//设置请求头的content-type
xmlhttp.setrequestheader("content-type","application/x-www-form-urlencoded");
//(3)发送数据
var ele_num1 = document.getelementsbyclassname("num1")[0];
var ele_num2 = document.getelementsbyclassname("num2")[0];
var ele_ret = document.getelementsbyclassname("ret")[0];
var ele_scrf = document.getelementsbyname("csrfmiddlewaretoken")[0];
var s1 = "num1="+ele_num1.value;
var s2 = "num2="+ele_num2.value;
var s3 = "&csrfmiddlewaretoken="+ele_scrf.value;
xmlhttp.send(s1+"&"+s2+s3); //请求数据
//(4)回调函数,success
xmlhttp.onreadystatechange = function () {
if (this.readystate==4&&this.status==200){
alert(this.responsetext);
ele_ret.value = this.responsetext
}
}
}
方式二====================================================
{# ===================json=============#}
var ele_btn=document.getelementsbyclassname("send_ajax")[0];
ele_btn.onclick=function () {
// 发送ajax
// (1) 获取 xmlhttprequest对象
xmlhttp = new xmlhttprequest();
// (2) 连接服务器
// get
//xmlhttp.open("get","/sendajax/?a=1&b=2");
// post
xmlhttp.open("post","/sendajax/");
// 设置请求头的content-type
var ele_csrf=document.getelementsbyname("csrfmiddlewaretoken")[0];
xmlhttp.setrequestheader("content-type","application/json");
xmlhttp.setrequestheader("x-csrftoken",ele_csrf.value); //利用js的方式避免forbidden的解决办法
// (3) 发送数据
var ele_num1 = document.getelementsbyclassname("num1")[0];
var ele_num2 = document.getelementsbyclassname("num2")[0];
var ele_ret = document.getelementsbyclassname("ret")[0];
var s1 = ele_num1.value;
var s2 = ele_num2.value;
xmlhttp.send(json.stringify(
{"num1":s1,"num2":s2}
)) ; // 请求体数据
// (4) 回调函数 success
xmlhttp.onreadystatechange = function() {
if(this.readystate==4 && this.status==200){
console.log(this.responsetext);
ele_ret.value = this.responsetext
}
};
}
</script>
views.py
def sendajax(request):
num1=request.post.get("num1")
num2 = request.post.get("num2")
ret = float(num1)+float(num2)
return httpresponse(ret)
js实现ajax小结
创建xmlhttprequest对象;
调用open()方法打开与服务器的连接;
调用send()方法发送请求;
为xmlhttprequest对象指定onreadystatechange事件函数,这个函数会在
xmlhttprequest的1、2、3、4,四种状态时被调用;
xmlhttprequest对象的5种状态,通常我们只关心4状态。
xmlhttprequest对象的status属性表示服务器状态码,它只有在readystate为4时才能获取到。
xmlhttprequest对象的responsetext属性表示服务器响应内容,它只有在
readystate为4时才能获取到!
总结:
- 如果"content-type"="application/json",发送的数据是对象形式的{},需要在body里面取数据,然后反序列化
= 如果"content-type"="application/x-www-form-urlencoded",发送的是/index/?name=haiyan&agee=20这样的数据,
如果是post请求需要在post里取数据,如果是get,在get里面取数据
实例(用户名是否已被注册)
7.1 功能介绍
在注册表单中,当用户填写了用户名后,把光标移开后,会自动向服务器发送异步请求。服务器返回true或false,返回true表示这个用户名已经被注册过,返回false表示没有注册过。
客户端得到服务器返回的结果后,确定是否在用户名文本框后显示“用户名已被注册”的错误信息!
7.2 案例分析
页面中给出注册表单;
在username表单字段中添加onblur事件,调用send()方法;
send()方法获取username表单字段的内容,向服务器发送异步请求,参数为username;
django 的视图函数:获取username参数,判断是否为“yuan”,如果是响应true,否则响应false
参考代码:
<script type="text/javascript">
function createxmlhttprequest() {
try {
return new xmlhttprequest();
} catch (e) {
try {
return new activexobject("msxml2.xmlhttp");
} catch (e) {
return new activexobject("microsoft.xmlhttp");
}
}
}
function send() {
var xmlhttp = createxmlhttprequest();
xmlhttp.onreadystatechange = function() {
if(xmlhttp.readystate == 4 && xmlhttp.status == 200) {
if(xmlhttp.responsetext == "true") {
document.getelementbyid("error").innertext = "用户名已被注册!";
document.getelementbyid("error").textcontent = "用户名已被注册!";
} else {
document.getelementbyid("error").innertext = "";
document.getelementbyid("error").textcontent = "";
}
}
};
xmlhttp.open("post", "/ajax_check/", true, "json");
xmlhttp.setrequestheader("content-type", "application/x-www-form-urlencoded");
var username = document.getelementbyid("username").value;
xmlhttp.send("username=" + username);
}
</script>
//--------------------------------------------------index.html
<h1>注册</h1>
<form action="" method="post">
用户名:<input id="username" type="text" name="username" onblur="send()"/><span id="error"></span><br/>
密 码:<input type="text" name="password"/><br/>
<input type="submit" value="注册"/>
</form>
//--------------------------------------------------views.py
from django.views.decorators.csrf import csrf_exempt
def login(request):
print('hello ajax')
return render(request,'index.html')
# return httpresponse('helloyuanhao')
@csrf_exempt
def ajax_check(request):
print('ok')
username=request.post.get('username',none)
if username=='yuan':
return httpresponse('true')
return httpresponse('false')
三、同源策略与jsonp
同源策略(same origin policy)是一种约定,它是浏览器最核心也最基本的安全功能,如果缺少了同源策略,则浏览器的正常功能可能都会受到影响。可以说web是构建在同源策略基础之上的,浏览器只是针对同源策略的一种实现。
同源策略,它是由netscape提出的一个著名的安全策略。现在所有支持javascript 的浏览器都会使用这个策略。所谓同源是指,域名,协议,端口相同。当一个浏览器的两个tab页中分别打开来 百度和谷歌的页面当浏览器的百度tab页执行一个脚本的时候会检查这个脚本是属于哪个页面的,即检查是否同源,只有和百度同源的脚本才会被执行。如果非同源,那么在请求数据时,浏览器会在控制台中报一个异常,提示拒绝访问。
jsonp(jsonpadding)
之前发ajax的时候都是在自己给自己的当前的项目下发
现在我们来实现跨域发。给别人的项目发数据,
创建两个项目,先来测试一下
项目一:
<body>
<h1>项目一</h1>
<button class="send_jsonp">jsonp</button>
<script>
$(".send_jsonp").click(function () {
$.ajax({
url:"http://127.0.0.1:8080/ajax_send2/", #去请求项目二中的url
success:function (data) {
console.log(data)
}
})
})
</script>
</body>
项目二:
=========================index.html===============
<h1>项目二</h1>
<button class="send_jsonp">jsonp</button>
<script>
$(".send_jsonp").click(function () {
$.ajax({
url:"/ajax_send2/",
success:function (data) {
console.log(data)
}
})
})
</script>
</body>
=========================views===============
from django.shortcuts import render,httpresponse
# create your views here.
def index(request):
return render(request, "index.html")
def ajax_send2(request):
print(222222)
return httpresponse("hello")
出现了一个错误,这是因为同源策略给限制了,这是游览器给我们报的一个错
(但是注意,项目2中的访问已经发生了,说明是浏览器对非同源请求返回的结果做了拦截。)
注意:a标签,form,img标签,引用cdn的css等也属于跨域(跨不同的域拿过来文件来使用),不是所有的请求都给做跨域,(为什么要进行跨域呢?因为我想用人家的数据,所以得去别人的url中去拿,借助script标签)
如果用script请求的时候也会报错,当你你返回的数据是一个return httpresponse(“项目二”)只是一个名字而已,js中如果有一个变量没有声明,就会报错。就像下面的这样了
<script src="http://127.0.0.1:8080/ajax_send2/">
项目二
</script>
只有发ajax的时候给拦截了,所以要解决的问题只是针对ajax请求能够实现跨域请求
解决同源策源的两个方法:
1、jsonp(将json数据填充进回调函数,这就是jsonp的json+padding的含义。)
jsonp是json用来跨域的一个东西。原理是通过script标签的跨域特性来绕过同源策略。
思考:这算怎么回事?
<script src="http://code.jquery.com/jquery-latest.js"></script>
借助script标签,实现跨域请求,示例:
所以只是单纯的返回一个也没有什么意义,我们需要的是数据
如下:可以返回一个字典,不过也可以返回其他的(简单的解决了跨域,利用script)
项目一:
<body>
<h1>项目一</h1>
<button class="send_jsonp">jsonp</button>
<script>
$(".send_jsonp").click(function () {
$.ajax({
url:"",
success:function (data) {
console.log(data)
}
})
});
function func(arg) {
console.log(arg)
}
</script>
<script src="http://127.0.0.1:8080/ajax_send2/"></script>
</body>
项目二:
def ajax_send2(request):
import json
print(222222)
# return httpresponse("func('name')")
s = {"name":"haiyan","age":12}
# return httpresponse("func('name')")
return httpresponse("func('%s')"%json.dumps(s)) #返回一个func()字符串,正好自己的ajax里面有个func函数,就去执行func函数了,
arg就是传的形参
这样就会取到值了
这其实就是jsonp的简单实现模式,或者说是jsonp的原型:创建一个回调函数,然后在远程服务上调用这个函数并且将json 数据形式作为参数传递,完成回调。
将json数据填充进回调函数,这就是jsonp的json+padding的含义。
但是以上的方式也有不足,回调函数的名字和返回的那个名字的一致。并且一般情况下,我们希望这个script标签能够动态的调用,而不是像上面因为固定在html里面所以没等页面显示就执行了,很不灵活。我们可以通过javascript动态的创建script标签,这样我们就可以灵活调用远程服务了。
解决办法:javascript动态的创建script标签
===========================jquery实现=====================
{# 创建一个script标签,让他请求一次,请求完了删除他#}
//动态生成一个script标签,直接可以定义个函数,放在函数里面
function add_script(url) {
var ele_script = $("<script>");
ele_script.attr("src",url);
ele_script.attr("id","script");
$("body").append(ele_script);
$("#script").remove()
}
$(".kuayu").click(function () {
add_script("http://127.0.0.1:8080/ajax_send2/")
});
</script>
==================js实现==========================
<button onclick="f()">sendajax</button>
<script>
function addscripttag(src){
var script = document.createelement('script');
script.setattribute("type","text/javascript");
script.src = src;
document.body.appendchild(script);
document.body.removechild(script);
}
function func(name){
alert("hello"+name)
}
function f(){
addscripttag("http://127.0.0.1:7766/sendajax/")
}
</script>
为了更加灵活,现在将你自己在客户端定义的回调函数的函数名传送给服务端,服务端则会返回以你定义的回调函数名的方法,将获取的json数据传入这个方法完成回调:
function f(){
addscripttag("http://127.0.0.1:7766/sendajax/?callbacks=func")
}
将视图views改为
def sendajax(request):
import json
dic={"k1":"v1"}
print("callbacks:",request.get.get("callbacks"))
callbacks=request.get.get("callbacks") #注意要在服务端得到回调函数名的名字
return httpresponse("%s('%s')"%(callbacks,json.dumps(dic)))
四、jquery对jsonp的实现
getjson
jquery框架也当然支持jsonp,可以使用$.getjson(url,[data],[callback])方法
<button onclick="f()">sendajax</button>
<script>
function f(){
$.getjson("http://127.0.0.1:7766/sendajax/?callbacks=?",function(arg){
alert("hello"+arg)
}); 匿名函数
}
</script>
8002的views不改动。
结果是一样的,要注意的是在url的后面必须添加一个callback参数,这样getjson方法才会知道是用jsonp方式去访问服务,callback后面的那个?是内部自动生成的一个回调函数名。
此外,如果说我们想指定自己的回调函数名,或者说服务上规定了固定回调函数名该怎么办呢?我们可以使用$.ajax方法来实现
$.ajax
<script>
function f(){
$.ajax({
url:"http://127.0.0.1:7766/sendajax/",
datatype:"jsonp",
jsonp: 'callbacks', #键
jsonpcallback:"sayhi" #函数的名字
}); } function sayhi(arg){ alert(arg); } </script>
8002的views不改动。
当然,最简单的形式还是通过回调函数来处理:
<script>
function f(){
$.ajax({
url:"http://127.0.0.1:7766/sendajax/",
datatype:"jsonp", //必须有,告诉server,这次访问要的是一个jsonp的结果。
jsonp: 'callbacks', //jquery帮助随机生成的:callbacks="wner"
success:function(data){
alert("hi "+data)
}
});
}
</script>
jsonp: 'callbacks'就是定义一个存放回调函数的键,jsonpcallback是前端定义好的回调函数方法名'sayhi',server端接受callback键对应值后就可以在其中填充数据打包返回了;
jsonpcallback参数可以不定义,jquery会自动定义一个随机名发过去,那前端就得用回调函数来处理对应数据了。利用jquery可以很方便的实现jsonp来进行跨域访问。
注意 jsonp一定是get请求
五、应用
// 跨域请求实例
$(".jiangxitv").click(function () {
$.ajax({
url:"http://www.jxntv.cn/data/jmd-jxtv2.html?callback=list&_=1454376870403",
datatype: 'jsonp',
jsonp: 'callback',
jsonpcallback: 'list',
success:function (data) {
console.log(data.data); // [{},{},{},{},{},{}]
week_list=data.data;
$.each(week_list,function (i,j) {
console.log(i,j); // 1 {week: "周一", list: array(19)}
s="<p>"+j.week+"列表</p>";
$(".show_list").append(s);
$.each(j.list,function (k,v) { // {time: "0030", name: "通宵剧场六集连播", link: "http://www.jxntv.cn/live/jxtv2.shtml"}
a="<p><a href='"+v.link+"'>"+v.name+"</a></p>";
$(".show_list").append(a);
})
})
}
})
})
相关推荐:
ajax的常用语法是什么
几种javascript实现原生ajax的方法
jquery一个ajax实例
以上就是js实现的ajax和同源策略详解的详细内容。