Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
194 views
in Technique[技术] by (71.8m points)

Cant get URL parameters in CodeIgniter

I tried to access an id from a URL "myfolder/mycontroller/mymethode/123" which I call into an AJAXcall.

I cannot access them, in "mycontroller" under the "mymethode". I tried to output $_GET, $_POST, $this->input->get(), $this->input->post(), but all array are empty.

In the Controller/mycontroller I have this

public function mymethode($listid=false)
{
      echo "listid: $listid";
      print_r($_GET);
      print_r($_POST);
      print_r($this->input->get());
      print_r($this->input->post());
}

The Ajax call is this and is ok with Status 200.

$.ajax({
                          url: http://mydomein.com/myfolder/mycontroller/mymethode/123,
                          type: "POST",
                          method: 'post',
                          data: form + "&" + additional_data + csrfName + "=" + csrfHash, 
                          dataType: 'json',
                          cache: false,                         
                         success: function(res){...

If I tried to open the URL directly, I have the same problem.

What can the reason for it be?

question from:https://stackoverflow.com/questions/66051036/cant-get-url-parameters-in-codeigniter

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use this (if this is not HMVC)

in route.php

# You may need first route since you're accepting null on method
$route['mymethode] = 'mycontroller/mymethode';
$route['mymethode/(:num)'] = 'mycontroller/mymethode/$1';

In AJAX

url: http://mydomein.com/mymethode/123,

Make sure your sites run without index.php

In controller

public function mymethode($listid=null)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...