【问题标题】:Calling Controller method from javascript using CodeIgniter使用 CodeIgniter 从 javascript 调用 Controller 方法
【发布时间】:2014-03-09 08:54:18
【问题描述】:

我正在使用 CodeIgniter ..我无法从控制器访问该方法

我的视图中有这个脚本

<script>
  function showHint(str){
    if (str.length==0){ 
        document.getElementById("txtHint").innerHTML="";
        return;
    }
    var xmlhttp=new XMLHttpRequest();
    xmlhttp.onreadystatechange=function(){
        if (xmlhttp.readyState==4 && xmlhttp.status==200){
               document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
         }
    }
    xmlhttp.open("GET","ajax/ajaxSearch",true);
    xmlhttp.send();
  }
  </script>

我有一个名为 ajax 的控制器,其方法是 ajaxSearch()

public function ajaxSearch(){
    echo "received";
}

文件夹结构

htdocs/
     AjaxTry/
        AjaxSearchingMVC/
            application/
                controller/
                    ajax.php                //controller
                          ajaxSearch()      //method
                views/
                    view_ajax.php           // here is where the script is written

这可能是什么问题?

【问题讨论】:

  • can't access 是什么意思?调试器控制台中是否有任何错误?
  • 无法访问该方法.. 它说.. HTTP/1.1 404 Not Found .. 它指向 my_base_url/ajax/ajaxSearch
  • 当然你也试过"/ajax/ajaxSearch"?
  • 我试了一下,url变成localhost/ajax/ajaxSearch
  • @Katherine:我不知道你是如何在 PhP 中做到这一点的,但在 ASP.NET MVC 中,我们将目标 url 从服务器发送到数据属性中的客户端,或者与服务器总是类似的无论发布到哪个服务器/域,都可以使用完整的 URL。在 ASP/NET MVC 中,我们有像 URL.Action('action', 'controller') 这样的服务器端 C# 助手,它返回完整且正确的 URL。我不知道 PhP 中的等价物是什么。

标签: javascript php codeigniter directory-structure


【解决方案1】:

我在项目中用于 ajax 请求的内容是形成如下所示的 Ajax URL:

  1. 在你的视图中放置一个全局变量,在头部,值设置为base_url(),如下所示:

    var base_url = <?php echo base_url(); ?>
    
  2. 现在在您的脚本中,调用您尝试访问的控制器操作,使用 base_url,如下所示:

    xmlhttp.open("GET", base_url + "ajax/ajaxSearch",true);
    

这将创建您的 ajax URL,例如 http://yourbaseurl/ajax/ajaxSearch,并希望为您解决问题!

注意

您的 base_url 必须类似于 http://localhost/yourprojectfolder/ 才能正常工作

【讨论】:

  • 首先base_url 不会是url,其次,这无济于事,因为没有像ajax/ajaxSearch 这样的文件
  • 而且这是不好的做法,所以混合 php 和 JavaScript 变量
  • @crypticous base_url will not be url 为什么会这样?
  • 因为你必须把它回显出来,但无论如何它也无济于事!
  • 好的,有错字。我添加了回声。并请解释“无论如何它都无济于事”。我一直在我的项目中这样做,并且一直运行良好..
【解决方案2】:

执行以下操作...

controller 中创建example.php 并保持ajax.php 不变。在视图中离开就像你已经拥有view_ajax.php

我们将使用 Ajax 从example.php 加载数据

你的ajax.php 应该是这样的

class ajax extends CI_Controller {

    public function index()
    {
        $this->load->helper('url'); // if you are going to use helpher don't forget to load it ( of course if you are not loading it by default )
        $this->load->view('view_ajax.php'); // in `view_ajax.php` you must have JavaScript code
    }
}

用于测试目的的 JavaScript 代码编写如下

<script>
var xmlhttp=new XMLHttpRequest();
    xmlhttp.onreadystatechange=function(){
        if (xmlhttp.readyState==4 && xmlhttp.status==200){
               alert(xmlhttp.responseText);
               console.log(xmlhttp.responseText);// you will see OKKK in console
         }
    }
xmlhttp.open("GET","../index.php/example",true); // first try `../index.php/example` ( extension depends if you enable/disable url rewrite in apache.conf ) , if this won't work then try base_url/index.php/example ( where you can specify base_url by static or with CodeIgniter helpher function )
xmlhttp.send();
</script>

嵌套台阶

example.php 应该是这样的

class example extends CI_Controller {
    public function index()
    {
        echo "OKKK";
    }
}

【讨论】:

  • 什么意思适用,没关系。出于测试目的,我建议您创建一些 example.php(或任何您的服务器端脚本语言)并在其中发布一些内容并将此 example.php 路径指定到 JavaScript 中并查看结果
  • 这段代码来自那个格式,它可以工作,但是我厌倦了把它改成mvc风格,现在它有问题了
  • @Katherine 你知道,从你的结构我看不出ajax/ajaxSearch在哪里
  • @Katherine 您的结构中的php 脚本在哪个文件中?你的JavaScript 写在哪个文件中?
  • 我更新了文件夹结构.. javascript 是在 view_ajax 中编写的,ajaxSearch 方法是在 ajax 控制器中
【解决方案3】:

在JS脚本中添加如下函数

function FetchData() {
    var valueFromClient = document.getElementById("ReplaceWithID").value;

alert("Received from client:"+valueFromClient );

    var xmlhttp;
    if (window.XMLHttpRequest) {
        // code for modern browsers
        xmlhttp = new XMLHttpRequest();
    } else {
        // code for old IE browsers
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange = function () {
        if (this.readyState == 4 && this.status == 200) {
            alert("Received from server:"+this.responseText);
        }
    };

    xmlhttp.open("POST", "http://localhost:52322/ControllerName/MethodName?CurrentC=" + valueFromClient , true);
    xmlhttp.send();
}
  1. 根据您的端口号更改Port : 52322
  2. 它是一个本地主机。因此,一旦您的网站上线,您可能需要更改它。
  3. 在视图上。加onChange(FetchData())

例如:

<select id="AnyIDHERE" onchange="updateProvince()"> </select>

【讨论】:

    猜你喜欢
    • 2019-10-08
    • 2012-07-23
    • 2012-08-28
    • 2011-09-14
    • 1970-01-01
    • 2018-04-04
    • 2015-02-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多