Javascript 如何在ajax调用中发送参数
我有一个电话。我需要向函数传递一些参数。这是我的AJAX调用Javascript 如何在ajax调用中发送参数,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我有一个电话。我需要向函数传递一些参数。这是我的AJAX调用 $.ajax({ url: 'lib/function.php', data: { action: 'getStoreSupplyItems', id: store_id, indent: 1 }, success: function(output) { //alert(output); $('#response').
$.ajax({
url: 'lib/function.php',
data: {
action: 'getStoreSupplyItems',
id: store_id,
indent: 1
},
success: function(output) {
//alert(output);
$('#response').html(output);
}
});
下面是我试图调用的后端函数定义:
function getStoreSupplyItems($category = '')
{
global $db;
$data = $_REQUEST;
$category = (!empty($category) ? ' AND cim.item_group_code IN ("'.$category.'") ' : '');
if ($data['id'] != "")
{
$store = $data['id'];
}
else
{
$store = $_SESSION['user']['store']['id'];
}
如何将一些参数传递给函数?我要传递的参数类似于“12,5,6”。要传递数据,您必须进行如下调用:
$.ajax({
method: "POST",
url: "lib/function.php",
data: { name: "John", location: "Boston" }
}).done(function( msg ) {
alert( "Data Saved: " + msg );
});
然后,在PHP函数中,您必须通过$_POST['name']和$_POST['location']访问params您可以在PHP文件中放入一个
switch
语句,然后调用传递参数的函数。像这样
switch($_REQUEST["action"]){
case "getStoreSupplyItems" :
getStoreSupplyItems("2,12,5");
break;
}
在getStoreSupplyItems
函数中,您可以通过$\u REQUEST
或$\u get
获取值作为参数,也可以使用其他get
参数
我希望这就是您想要的。您能更具体地说明您需要什么吗?只需在您提供给
数据的对象中添加另一个属性即可。
。@Rory McCrossan:这是一个解决方案。但如果这样做,我需要更改后端中$category的条件。它会影响其他一些功能。我需要的是php,我需要在$category字段中获取“2,12,5”这个字符串。您是否使用了一些框架?例如,在Yii中,您可以通过GET传递参数,并将其反馈给动作函数。这是一个非常好的函数。这是很有用的,我可以再问一个问题吗,比如如果参数在变化,会怎样变化?您是否在ajax请求中发送该参数?是的,我需要从ajax请求中发送它。对于不同的事件,我需要发送不同的参数。然后您可以简单地执行此操作getStoreSupplyItems($\u请求[“param\u其中包含值])代码>