【问题标题】:How to call controller from another url with post data?如何使用发布数据从另一个 url 调用控制器?
【发布时间】:2015-07-17 09:57:41
【问题描述】:

我的项目使用 codeigniter 时遇到问题。 我想使用发布数据从另一个 url(没有我的项目)运行一些控制器。 我正在使用 codeigniter 框架? 那么有什么办法吗? 请帮我解决这个问题...

【问题讨论】:

标签: codeigniter


【解决方案1】:

我能想到的只是使用 ajax ,但是不建议使用 javascript 作为控制器的一部分

在控制器 A 中

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class controllerA extends CI_Controller {

     function __construct()
    {
        // you can put the ajax call here if you want it to run each time you call this controller
    }

    public function ajaxcall()
    {
    // make sure you didn't call jquery before so you won't have conflicting scripts
    echo '<script src="https://code.jquery.com/jquery-1.11.3.min.js"> </script>';

    // now we use ajax to post to the controller B 
    echo
    '<script>
    var target_url = "http://www.example.com/projectB/controllerB"
    var Data = {user_id:542,name:"Baci"};
    $.ajax(
            {
                url : target_url,
                type: "POST",
                data : Data,
                success: function(data)
                {

                    alert("all right request was sent via Ajax  ");

                },
                error: function(jqXHR, textStatus, errorThrown)
                {
                    alert("request failed !  ");
                }
            });
    </script>
    ' ;

    // continue your code on your controller while the ajax call is being sent
    }
}

【讨论】:

  • 我不使用任何形式。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-11
  • 1970-01-01
  • 1970-01-01
  • 2015-09-07
  • 1970-01-01
相关资源
最近更新 更多