源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

json跨域调用python的方法详解

  • 时间:2022-10-31 05:14 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:json跨域调用python的方法详解
本文实例讲述了json跨域调用python的方法。分享给大家供大家参考,具体如下: [b]客户端:[/b]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  <html xmlns="http://www.w3.org/1999/xhtml">
  <head>
  <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
  <title>jQuery-跨域请求</title>
  <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
  <script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
  </head>
   <script type="text/javascript">
  jQuery(document).ready(function(){
    $.ajax({
      type : "GET",
      url : "http://10.13.38.43:1234/?id=10&callback=?",
      dataType : "jsonp",
      jsonp: 'callback',
      success : function(json){
          alert(json.account);
        //$('#msg_box').html(json);
        //return true;
      }
    });
  });
  </script>
   <body>
  <div id="msg_box"></div>
  </body>
  </html>

[b]服务端[/b]
import web
urls=('/','Index',)
class Index:
    def GET(self):
      inputdata=web.input()
      mycallbackfun=inputdata.callback
      #return 'hello' +inputdata.id
      return mycallbackfun+'({"account":"XX","passed":"true","error":"null"})'
app = web.application(urls, globals())
if __name__=='__main__':
    app.run()

[b]附:jquery跨域请求方法简介[/b] 这里介绍jQuery跨域请求方法,并提供简单的示例代码供参考。 项目中关于ajax jsonp的使用,出现了问题:可以成功获得请求结果,但没有执行success方法,总算搞定了,记录一下。
function TestAjax()
{
  $.ajax({
    type : "get",
    async : false,
    url : "ajaxHandler.ashx", //实际上访问时产生的地址为: ajax.ashx?callbackfun=jsonpCallback&id=10
    data : {id : 10},
    cache : false, //默认值true
    dataType : "jsonp",
    jsonp: "callbackfun",//传递给请求处理程序或页面的,用以获得jsonp回调函数名的参数名(默认为:callback)
    jsonpCallback:"jsonpCallback",
      //自定义的jsonp回调函数名称,默认为jQuery自动生成的随机函数名
      //如果这里自定了jsonp的回调函数,则success函数则不起作用;否则success将起作用
    success : function(json){
      alert(json.message);
    },
    error:function(){
      alert("erroe");
    }
  });
}
function jsonpCallback(data) //回调函数
{
  alert(data.message); //
}
public class ajaxHandler : IHttpHandler
{
  public void ProcessRequest (HttpContext context) {
    context.Response.ContentType = "text/plain";
    string callbackfun = context.Request["callbackfun"];
    context.Response.Write(callbackfun + "({name:\"John\", message:\"hello John\"})");
    context.Response.End();
  }
  public bool IsReusable {get {return false;}
}

[b]ajax请求参数说明:[/b] dataType string 服务器返回的数据类型。 如果不指定,jQuery 将自动根据 HTTP 包 MIME 信息来智能判断,比如XML MIME类型就被识别为XML。 可用值: "xml": 返回 XML 文档,可用 jQuery 处理。 "html": 返回纯文本 HTML 信息;包含的script标签会在插入dom时执行。 "script": 返回纯文本 JavaScript 代码。不会自动缓存结果。除非设置了"cache"参数。 注意:在远程请求时(不在同一个域下),所有POST请求都将转为GET请求。(因为将使用DOM的script标签来加载) "json": 返回 JSON 数据 。 "text": 返回纯文本字符串 "jsonp":jsonp格式。使用jsonp形式调用函数时,访问url时会自动将url后面添加上如"callback=callbackFunName" 以执行回调函数(callbackFunName)。 jsonp string 在一个jsonp请求中重写回调函数的名字。这个值用来替代在"callback=?"这种get或post请求中url参数里的"callback"部分,比如 jsonp:'callbackfun' 则将会生成"callbackfun=?"传给服务器。 jsonpCallback String 此参数为jsonp请求指定一个回调函数名。 这个值将用来取代jQuery自动生成的随机函数名。 即上面"callback=?"中的问号部分。 这主要用来让jQuery生成度独特的函数名,这样请求更容易,也能方便地提供回调函数和错误处理。 也可以在想让浏览器缓存GET请求的时候,指定这个回调函数名。 ajax jsonp与普通的ajax请求的主要区别在于——请求响应结果的处理。如上面代码所示的响应结果为:
jsonpCallback({ name:"world",message:"hello world"});

实际上就是调用jsonp回调函数jsonpCallback,并将要响应的字符串或json传入此方法,关于自定了jsonp的回调函数, success函数则不起作用,大概其底层的实现(当然这是默认的回调函数的时候,否则就不会执行success的方法吧):
function default_jsonpCallback(data)
{
  success(data); //在默认的回调方法中执行
}

[b]PS:关于json操作,这里再为大家推荐几款比较实用的json在线工具供大家参考使用:[/b] [b]在线JSON代码检验、检验、美化、格式化工具: [/b][url=http://tools.jb51.net/code/json]http://tools.jb51.net/code/json[/url] [b]JSON[/b][b]在线格式化工具: [/b][url=http://tools.jb51.net/code/jsonformat]http://tools.jb51.net/code/jsonformat[/url] [b]在线XML/JSON互相转换工具: [/b][url=http://tools.jb51.net/code/xmljson]http://tools.jb51.net/code/xmljson[/url] [b]json[/b][b]代码在线格式化/美化/压缩/编辑/转换工具: [/b][url=http://tools.jb51.net/code/jsoncodeformat]http://tools.jb51.net/code/jsoncodeformat[/url] [b]在线json压缩/转义工具:[/b] [url=http://tools.jb51.net/code/json_yasuo_trans]http://tools.jb51.net/code/json_yasuo_trans[/url] [b]C语言风格/HTML/CSS/json代码格式化美化工具: [/b][url=http://tools.jb51.net/code/ccode_html_css_json]http://tools.jb51.net/code/ccode_html_css_json[/url] 更多Python相关内容感兴趣的读者可查看本站专题:《[url=http://www.1sucai.cn/Special/800.htm]Python操作json技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/788.htm]Python编码操作技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/645.htm]Python图片操作技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/663.htm]Python数据结构与算法教程[/url]》、《[url=http://www.1sucai.cn/Special/648.htm]Python Socket编程技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/642.htm]Python函数使用技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/636.htm]Python字符串操作技巧汇总[/url]》、《[url=http://www.1sucai.cn/Special/520.htm]Python入门与进阶经典教程[/url]》及《[url=http://www.1sucai.cn/Special/516.htm]Python文件与目录操作技巧汇总[/url]》 希望本文所述对大家Python程序设计有所帮助。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部