前端_jsonp用法
JSP的后台例子:
String paramS= request.getParameter("paramStr");
String callback = request.getParameter("callback");
out.println(callback + "('" + serverSide.exec(paramS) + "')");
$.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;
});
});
=============================================jquery用法
<script>
$(document).ready(function(){
$.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;
});
});
});
</script>
jsoncallback=?,其中?会自动替换为function(data)函数。
2. $.ajax
$.ajax({
dataType: 'jsonp',
data: 'id=10',
jsonp: 'jsonp_callback',
url: 'http://www.yiwuku.com/getdata',
success: function () {
// do stuff
},
});
http://dagmom.iteye.com/blog/1632340 问题解决登陆地址
================================================解决登陆的问题
$.getJson('http://192.168.1.101:7080/B/loginAction.action?loginName=www&password=wwww&jsonCallback=?',function(data){
if(data && data[0] && data[0].status == '1'){
location.replace('http://192.168.1.101:7080/B/loginAction.action');
});
服务器端的代码我就不写了,无非就是通过session和cookie来判断是否登录。
<script type="text/javascript">
$(document).ready(function(){
$.ajax({
url:'http://192.168.9.5/jsonp_proc.asp',
dataType:"jsonp",
jsonp:"jsonpcallback",
success:function(data){
var $ul = $("<ul></ul>");
$.each(data,function(i,v){
$("<li/>").text(v["id"] + " " + v["name"]).appendTo($ul)
});
$("#res").append($ul);
}
});
});
</script>
//后台代码
jsonp = request("jsonpcallback")
str = "[{""id"":""1"",""name"":""测试1""},{""id"":""2"",""name"":""测试2""}]"
str = jsonp & "(" & str&")"
response.Write(str)
///////////////////////////
$.getJSON( "http://192.168.0.66/services/WebService1.asmx/ws?callback=?", { name: "ff", time: "2pm" }, function(data) { alert(decodeURI(data.msg)) } );
////后台代码
///////
样式代码
把 jsonp 换成 json
dataType: 'json',
jquery 会把收到的字条串解析成对象,
如:目标ulr会得下面字符 {"s":"1"}
data = {"s":"1"}
data.s == 1
////
public void ws(string name,string time) { HttpRequest Request = HttpContext.Current.Request; string callback = Request["callback"]; HttpResponse Response = HttpContext.Current.Response; Response.Write(callback + "({msg:'this is"+name+"jsonp'})"); Response.End(); }