$(function(){
$("#submit").click(function(){
vardata= {
name: $("#name").val(),
id: $("#id").val()
};
$.ajax({
type: 'POST',
data: data,
url: 'http://localhost:3000/ajax/deal',
dataType: 'json',
cache: false,
timeout: 5000,
success: function(data){
console.log(data)
},
error: function(jqXHR,textStatus,errorThrown){
console.log('error '+ textStatus+ ' '+ errorThrown);
}
});
});
});
pp.post('/ajax/deal',function(req,res){
console.log("server accept: ",req.body.name,req.body.id)
vardata= {
name: req.body.name+ ' - server 3000 process',
id: req.body.id+ ' - server 3000 process'
}
res.send(data)
res.end()
})
$.ajax({
...
url: 'http://localhost:3001/ajax/deal',
...
});
app.post('/ajax/deal',function(req,res){
console.log("server accept: ",req.body.name,req.body.id)
vardata= {
name: req.body.name+ ' - server 3001 process',
id: req.body.id+ ' - server 3001 process'
}
res.send(data)
res.end()
})
server accept: chiaki 3001
JSONP (JSON with Padding or JSON-P) is a JSON extension used by web developers to overcome the cross-domain restrictions imposed by browsers’ same-origin policy that limits access to resources retrieved from origins other than the one the page was served by. In layman’s terms, one website cannot just simply access the data from another website.
It was developed because handling a browsers’ same origin policy can be difficult, so using JSONP abstracts the difficulties and makes it easier.
JSON stands for “Java Object Notation”, a format by which object fields are represented as key-value pairs which is used to represent data.
// 回调函数
functionjsonpCallback(data){
console.log("jsonpCallback: "+ data.name)
}
$("#submit").click(function(){
vardata= {
name: $("#name").val(),
id: $("#id").val()
};
$.ajax({
url: 'http://localhost:3001/ajax/deal',
data: data,
dataType: 'jsonp',
cache: false,
timeout: 5000,
// jsonp 字段含义为服务器通过什么字段获取回调函数的名称
jsonp: 'callback',
// 声明本地回调函数的名称,jquery 默认随机生成一个函数名称
jsonpCallback: 'jsonpCallback',
success: function(data){
console.log("ajax success callback: "+ data.name)
},
error: function(jqXHR,textStatus,errorThrown){
console.log(textStatus+ ' '+ errorThrown);
}
});
});
app.get('/ajax/deal',function(req,res){
console.log("server accept: ",req.query.name,req.query.id)
vardata= "{"+ "name:'"+ req.query.name+ " - server 3001 process',"+ "id:'"+ req.query.id+ " - server 3001 process'"+"}"
varcallback= req.query.callback
varjsonp= callback+ '('+ data+ ')'
console.log(jsonp)
res.send(jsonp)
res.end()
})
{
name: "chiaki",
id": "3001"
}
GET /ajax/deal?callback=jsonpCallback&name=chiaki&id=3001&_=1473164876032 HTTP/1.1
< src = 'http://localhost:3001/ajax/deal?callback=jsonpCallback&name=chiaki&id=3001&_=1473164876032'></>
functionjsonpCallback(data){
console.log("jsonpCallback: "+data.name)
}
< src = 'http://localhost:3001/jsonServerResponse?jsonp=jsonpCallback'></>
app.get('/jsonServerResponse',function(req,res){
varcb= req.query.jsonp
console.log(cb)
vardata= 'var data = {'+ 'name: $("#name").val() + " - server 3001 jsonp process",'+ 'id: $("#id").val() + " - server 3001 jsonp process"'+ '};'
vardebug= 'console.log(data);'
varcallback= '$("#submit").click(function() {'+ data+ cb+ '(data);'+ debug+ '});'
res.send(callback)
res.end()
})
$(function(){
$("#submit").click(function(){
vardata= {
name: $("#name").val(),
id: $("#id").val()
};
$.ajax({
type: 'POST',
data: data,
url: 'http://localhost:3001/cors',
dataType: 'json',
cache: false,
timeout: 5000,
success: function(data){
console.log(data)
},
error: function(jqXHR,textStatus,errorThrown){
console.log('error '+ textStatus+ ' '+ errorThrown);
}
});
});
});
app.post('/cors',function(req,res){
res.header("Access-Control-Allow-Origin","*");
res.header("Access-Control-Allow-Headers","X-Requested-With");
res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
res.header("X-Powered-By",' 3.2.1')
res.header("Content-Type","application/json;charset=utf-8");
vardata= {
name: req.body.name+ ' - server 3001 cors process',
id: req.body.id+ ' - server 3001 cors process'
}
console.log(data)
res.send(data)
res.end()
})
本文为 @ 21CTO 创作并授权 21CTO 发布,未经许可,请勿转载。
内容授权事宜请您联系 webmaster@21cto.com或关注 21CTO 公众号。
该文观点仅代表作者本人,21CTO 平台仅提供信息存储空间服务。