Codeigniter路由。 AJAX使用javascript获取参数

时间:2013-02-23 12:54:53

标签: javascript ajax codeigniter

我希望这与Codeigniter合作:

name = encodeURIComponent( document.getElementById("myName").value);

xmlHttp.open("GET", "quickstart.php?name=" + name, true);

xmlHttp.onreadystatechange = handleServerResponse; //not relevant for question

xmlHttp.send(null);

我创建了一个带有参数功能的控制器,并更改了以前的代码:

xmlHttp.open("GET", "quickstart.php?name=" + name, true);

xmlHttp.open("GET", "ajax/quickstart/"+name, true);

我使用这条路线(但不起作用):

$route['ajax'] = 'ajax';
$route['ajax/quickstart'] = 'ajax/quickstart';
$route['ajax/quickstart/([A-Za-z0-9])+'] = 'ajax/quickstart/$1';

我遇到的问题是我只写了最后一封信。例如,如果我写“name”,只有“e”它作为参数传递。但所有的话都被发送了。 我的控制器功能如下:

public function quickstart($name='')
{
// we'll generate XML output
header('Content-Type: text/xml');
// generate XML header
echo '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>';
// create the <response> element
echo '<response>';
// retrieve the user name
//$name = $this->input->get('name');

// generate output depending on the user name received from client
$userNames = array('YODA', 'AUDRA', 'BOGDAN');
if (in_array(strtoupper($name), $userNames))
    echo 'Hello, master ' . htmlentities($name) . '!';
else if (trim($name) == '')
    echo 'Stranger, please tell me your name!';
else

echo htmlentities($name) . ', I don\'t know you!';
// close the <response> element
echo '</response>';
}

1 个答案:

答案 0 :(得分:1)

仅限使用

$route['ajax/quickstart/(:any)'] = "ajax/quickstart/$1";

Documentation