我正在尝试使用ajax将变量发布到php控制器中:
$(".inp_pr").keypress(function(f) {
if (f.which == 13) {
dataString = 'qwe';
$.ajax({
type: "POST",
url: "/prwrk/",
data: 'dataString=' + dataString,
success: function(data) {
alert('<?php echo($data)?>');
}
});
event.preventDefault();
}
});
控制器来源:
function action_index()
{
$data=$_POST['dataString'];
$this->view->generate('prwrk_view.php', 'template_view.php',$data);
}
Ajax发布变量成功,但控制器没有它。我认为,这将不是正确的网址,但它不能与控制器文件的完整网址一起工作。
路由器来源:
class Route
{
static function start()
{
$controller_name = 'Main';
$action_name = 'index';
$routes = explode('/', $_SERVER['REQUEST_URI']);
if ( !empty($routes[1]) )
{
$controller_name = $routes[1];
}
if ( !empty($routes[2]) )
{
$action_name = $routes[2];
}
$model_name = 'Model_'.$controller_name;
$controller_name = 'Controller_'.$controller_name;
$action_name = 'action_'.$action_name;
$model_file = strtolower($model_name).'.php';
$model_path = "application/models/".$model_file;
if(file_exists($model_path))
{
include "application/models/".$model_file;
}
$controller_file = strtolower($controller_name).'.php';
$controller_path = "application/controllers/".$controller_file;
if(file_exists($controller_path))
{
include "application/controllers/".$controller_file;
}
else
{
Route::ErrorPage404();
}
$controller = new $controller_name;
$action = $action_name;
if(method_exists($controller, $action))
{
$controller->$action();
}
else
{
Route::ErrorPage404();
}
}
function ErrorPage404()
{
$host = 'http://'.$_SERVER['HTTP_HOST'].'/';
header('HTTP/1.1 404 Not Found');
header("Status: 404 Not Found");
header('Location:'.$host.'404');
}
}
如何将变量post到我的php控制器?
发布于 2016-05-25 11:53:27
更改为此
data:$("#formID").serialize(),
它通过ajax提交普通表单
发布于 2016-05-25 12:02:36
请尝试以下操作
$(".inp_pr").keypress(function(f) {
if (f.which == 13) {
dataString = 'qwe';
$.ajax({
type: "POST",
url: "/prwrk/",
data: {dataString:dataString},
success: function(data) {
$('body').append(data);//change the body to your dom element
}
});
}
});
https://stackoverflow.com/questions/37427235
复制相似问题