ajax通过post方式传参给后台controller,怎么获取传过来的参数

WBOY
Release: 2016-06-20 12:28:57
Original
1910 people have browsed it

ajax:
 $.ajax({
             url:'index.php?c=MapsApi&m=getLocation',
             type: 'post',
//           dataType: 'json',
             timeout: 3000,
             data:{name_province:name_province},
             success: function(msg){
               alert("dddd"+unescape(msg));
             },
             error: function(e){
                    alert(JSON.stringify(e));
                }
             });
    
用php实现


回复讨论(解决方案)

ajax用post提交,在控制器就用$_POST变量获取啊(也可以用$_REQUEST)。
print_r($_POST);

  url:'index.php?c=MapsApi&m=getLocation',
c/m参数用$_GET

 data:{name_province:name_province},

name_province用$_POST

其实ajax提交和表单提交数据一样,只是ajax请求不会控制浏览器跳转如果你服务器设置了3xx响应头,而是直接获取跳转到的页面的html代码

echo $_POST['name_province'];
?>

$_POST['name_province']

 type: 'post',  表示使用POST
 data:{name_province:name_province}, 参数与值

所以php获取可以这样写

<?php$data = isset($_POST['name_province'])? $_POST['name_province'] : '';echo $data;?>
Copy after login

现在已经解决了,我用的事get方式:
URL格式是 
                url:'index.php?c=MapsApi&m=getLocation&name_province='+name_province,
             type: 'get',
后台用$_GET['name_province']可以得到值;
get方式需要将url拼接,将所得数据返回给ajax时用的是exit('json_encode($info)');

$_POST['name_province']

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template