search
Homephp教程php手册PHP开发框架kohana中处理ajax请求的例子

这篇文章主要介绍了PHP开发框架kohana中处理ajax请求的例子,kohana是一个PHP5开发框架,需要的朋友可以参考下

今天分享的是在kohana中处理页面的ajax请求.2步搞定.前提是你的kohana框架已经可以正确运行了,注意下.

1.页面发出请求.

现在主流的javascript框架非jQuery莫属.jQuery对ajax请求也作了封装,这里就以jQuery为例来写个.demo是获取后台json串,并且用each处理.代码大部分是从jqapi搞来的,准确也方便.

$.ajax({ url: "/test/json",//test是控制器,json是action,带/是相对站点根目录的意思 dataType:json, // data: 这里的写法一般是拼字符串,'id=1&name=jack'这种. success: function(data){ var items = []; $.each(data, function(key, val) { items.push('

  • ' + val + '
  • '); }); $('
      ', { 'class': 'my-new-list', html: items.join('') }).appendTo('body'); } });

      2.kohana中的处理,返回json串.上代码

      public function action_json() { $this -> auto_render = FALSE;//不需要view if ($this -> request -> is_ajax()) //判断是否为ajax请求 { //get $arr here. echo json_encode($arr);//建议这样写,避免0或其他情况. exit; } // json 只支持 utf-8 编码,这点很重要,切记啊!!! }

      ok,相信看完这2段代码,kohana中处理ajax请求,你肯定懂的.

      PS:前台js必须是utf-8编码,要注意哦,亲.

      Statement
      The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

      Hot AI Tools

      Undresser.AI Undress

      Undresser.AI Undress

      AI-powered app for creating realistic nude photos

      AI Clothes Remover

      AI Clothes Remover

      Online AI tool for removing clothes from photos.

      Undress AI Tool

      Undress AI Tool

      Undress images for free

      Clothoff.io

      Clothoff.io

      AI clothes remover

      Video Face Swap

      Video Face Swap

      Swap faces in any video effortlessly with our completely free AI face swap tool!

      Hot Tools

      SublimeText3 Linux new version

      SublimeText3 Linux new version

      SublimeText3 Linux latest version

      Dreamweaver Mac version

      Dreamweaver Mac version

      Visual web development tools

      ZendStudio 13.5.1 Mac

      ZendStudio 13.5.1 Mac

      Powerful PHP integrated development environment

      SecLists

      SecLists

      SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

      SublimeText3 Mac version

      SublimeText3 Mac version

      God-level code editing software (SublimeText3)