簡體   English   中英

Codeigniter中的Ajax請求

[英]Ajax request in Codeigniter

我一直在嘗試在codeigniter中創建一個ajax請求。 我已經看到了這個問題: 簡單的Ajax / Codeigniter請求,但是由於人們在Javascript中使用PHP的答案,我無法理解這一點。 我不知道這是有可能的,但是我嘗試了一下,但似乎未執行PHP。

所以這是我的問題:

  1. 真的可以在Javascript中使用PHP,還是我弄錯了?
  2. 在Codeigniter中執行Ajax請求正確方法是什么? 我嘗試了以下內容:

var param = {name : event_name, date : event_date, time : event_time};

            $.ajax({
                // As seen from the question here at stackoverflow.
                url : "<?php echo base_url('event/new_event'); ?>",
                type : 'POST',
                data : param,
                beforeSend : function(){ },
                success : function(){
                    alert("Event created! Feel free to add as much details as you want.");
                    namebox.val("");
                    $("#new-event-modal").find(".close").trigger('click');
                    window.location.href = "<php echo base_url('user/dashboard'); ?>";
                },
                complete : function(){ },
                error : function(){ }
            });

我知道有可能在請求中對URL進行硬編碼,但這不是一個好習慣!

實現此目的最簡單的方法是使用一些jquery:

function getBaseUrl() {
    var l = window.location;
    var base_url = l.protocol + "//" + l.host + "/" + l.pathname.split('/')[1];
    return base_url;
}

var postdata = {name : event_name, date : event_date, time : event_time};
var url = getBaseUrl()+"/event/new_event";

$.post(url, postdata, function(result){
  ...alert(result);
});

或通過緩存直接從JS調用它:

<script> 

    var test = "<?php echo base_url(); ?>"+"event/new_event";
    alert(test);

</script>

這是我要使用的一個骯臟的技巧

  1. 在頁面上的某處創建一個隱藏字段 ,並在此頁面加載時回顯base_url()作為該隱藏字段的值
  2. 現在,當您要發出ajax請求時, 訪問該隱藏字段並獲取base url並根據需要使用它。

正確的方法始終是最簡單的方法,如果尚未使用Jquery,則無需在客戶端中導入它。

這是你的控制器

<?php if (!defined('BASEPATH')) die();
class Example_ctrl extends CI_Controller {

    public function ajax_echo()
    {
        // get the ajax input
        $input = json_decode(file_get_contents('php://input'));

        // $input can be accessed like an object
        $password = $input->password;
        $name = $input->name;

        // you can encode data back to JSON
        $output = json_encode($input);

        // and the response goes back!
        echo($output);
    }
}
?>

這進入您的客戶

<script>
    // here's the data you will send
    var my_data = {name: "Smith", password: "abc123"};

    // create the request object
    var xhr = new XMLHttpRequest();

    // open the object to the required url
    xhr.open("POST", "example_ctrl/ajax_echo", true);

    // on success, alert the response
    xhr.onreadystatechange = function () {
        if (xhr.readyState != 4 || xhr.status != 200)
            return;
        alert("Success: " + xhr.responseText);
        };

    // encode in JSON and send the string 
    xhr.send(JSON.stringify(my_data));
</script>

沒有更好的方法可以做到這一點。

不能從外部javascript文件執行php代碼。

嘗試以下任何一種方法:-

1)base_url()不會改變,最好將其存儲在cookie中,然后以服務器端代碼和客戶端代碼訪問

2)您可以在本地存儲中存儲相同的base_url(),它將在您的外部JavaScript文件中可用

希望它能對您有所幫助:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM