如何使用发布数据从另一个URL调用控制器?

时间:2015-07-17 09:57:41

标签: codeigniter

我的项目使用codeigniter有问题。 我想从另一个url(没有我的项目)运行一些控制器来发布数据。 我使用codeigniter框架? 有没有办法做到这一点? 请帮我解决这个问题...

1 个答案:

答案 0 :(得分:0)

我能想到的只是使用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
    }
}