新手 Ajax 和 PHP 获取参数
newbie ajax and php get parameters
我需要帮助来做一些小事,但我不知道如何解决它。我有一个包含 ajax 的 javascript 文件,如下所示
$.ajax({
data: "mc_id="+someid,
url: "includes/getDataPrs.php",
type: "GET",
dataType: "json",
async: false,
success: function(msg){
//some function here
}
});
在getDataPrs中.php
<?php
include_once 'db_connect.php';
include_once 'functions.php';
sec_session_start();
header('Content-Type: application/json');
$id = null;
$date = null;
$limit = 0;
if (isset($_GET['mc_id'])) {
$id = $_GET['mc_id'];
}
//some process here $data
echo json_encode($data);
我可以从$_GET['mc_id']
获取数据,但是当我需要更多数据并且像这样更改javascript中的参数时
$.ajax({
data: "{'mc_id':'"+someid+"','limit':'"+somelimit+"'}",
url: "includes/getDataPrs.php",
然后我在 php $_GET['mc_id']
或 $_GET['limit']
中一无所获在我绝望地解决它时,我输入了网址"includes/getDataPrs.php?mc_id=someid&limit=somelimit
我非常感谢的任何意见或建议提前感谢
在ajax
中传递多个变量应该是这样的
$.ajax({
data: {mc_id: someid, limit: some_limit},
url: "includes/getDataPrs.php",
type: "GET",
dataType: "json",
async: false,
success: function(msg){
//some function here
}
});
使用data: {mc_id: someid, limit: some_limit}
总是更好,因为它会像对象本身一样对待。
尝试使用以下语法在 ajax 函数中发送数据:
...
data:{mc_id:someid,limit:somelimit},
...
不使用引号。
更改自
data: "{'mc_id':'"+someid+"','limit':'"+somelimit+"'}",
自
data: "mc_id="+someid+"&limit="+somelimit,
按照以下步骤操作:
1) Replace `data: "mc_id="+someid,` with `data: { mc_id: someid},`. (required)
2) Now you can get your data in PHP file like `$_POST['mc_id']` (optional). It is better to use `type: 'POST'`in your jQuery code.
所以下面是你的整个代码:
$.ajax({
data: { mc_id: someid},
url: "includes/getDataPrs.php",
type: 'POST',
dataType: "json",
async: false,
success: function(msg){
//some function here
}});
在getDataPrs中.php
<?php
include_once 'db_connect.php';
include_once 'functions.php';
sec_session_start();
header('Content-Type: application/json');
$id = null;
$date = null;
$limit = 0;
if (isset($_POST['mc_id'])) {
$id = $_POST['mc_id'];
}
//some process here $data
echo json_encode($data);
?>
你试试这个
$.ajax({
type: "POST",
url: "includes/getDataPrs.php",
data: {'mc_id': someid, 'limit': somelimit}.done(function (response) {
//
});
});
相关文章:
- ROR:如何在不重新加载浏览器的情况下从控制器获取参数
- 如何在reactrouterv2.+中从URL获取参数
- 如何获取参数的值
- 从从字符串创建的函数中获取参数列表
- jquery从url中获取参数也会在控制台日志中留下url
- 从Javascript中的URL获取参数
- IFrame 从不同域上的父 url 获取参数
- 如何使用React Router在Route Handler上获取参数
- Angularjs-如何从URL获取参数
- 从外部函数获取参数名称
- PHP从带有url的变量中获取参数
- 是否可以在 JavaScript 中按函数名称获取参数
- JS .replace(str, str):如何获取参数 1 中的变量并全局工作
- 从页面网址获取参数
- 如何在 router.use 中定义参数时获取参数
- 如何从 JavaScript 中的 JSON 对象获取参数的名称
- 我将一个函数(本身有一个参数)作为参数传递给另一个函数以及如何获取参数
- 如何获取参数对象的子集?(关于具有可变大小的参数)
- Javascript对象通信并从其他对象获取参数
- Websocket 从查询中获取参数