2012-08-13 32 views
1

我想提出一个AJAX调用一个控制器上的从视图这样如何在ajax请求后从控制器中返回单个值?的Joomla 2.5

jQuery.ajax({ 
       type: "POST", 
       url: "index.php?option=com_virtuemart&view=participate&task=participate_request&virtuemart_product_id=2&virtuemart_category_id=5&tmpl=component&Itemid=101", 
       data: { name: "John", location: "Boston" } 
      }).done(function(msg) { 
       alert("Data Saved: " + msg); 
     }); 

,并在CONTROLER 我有一个任务方法声明如下

public function participate_request(){   
     echo 'test'; 
     return false; 
    } 

我得到这个响应

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="el-gr" lang="el-gr" > 
<head> 
    <meta http-equiv="content-type" content="text/html; charset=utf-8" /> 
    <meta name="generator" content="Joomla! - Open Source Content Management" /> 
    <title>offer_e_shop_1</title> 
    <link rel="stylesheet" href="/offer_e_shop_1/components/com_virtuemart/assets//css/facebox.css" type="text/css" /> 
    <link rel="stylesheet" href="/offer_e_shop_1/components/com_virtuemart/assets//css/vmsite-ltr.css" type="text/css" /> 
    <script src="/offer_e_shop_1/components/com_virtuemart/assets/js/jquery.min.js" type="text/javascript"></script> 
    <script src="/offer_e_shop_1/components/com_virtuemart/assets//js/jquery.noConflict.js" type="text/javascript"></script> 
    <script src="/offer_e_shop_1/components/com_virtuemart/assets//js/jquery-ui-timepicker-addon.js" type="text/javascript"></script> 
    <script src="/offer_e_shop_1/components/com_virtuemart/assets//js/jquery-ui-sliderAccess.js" type="text/javascript"></script> 
    <script src="/offer_e_shop_1/components/com_virtuemart/assets//js/vmsite.js" type="text/javascript"></script> 
    <script src="/offer_e_shop_1/components/com_virtuemart/assets//js/facebox.js" type="text/javascript"></script> 
    <script src="/offer_e_shop_1/components/com_virtuemart/assets//js/vmprices.js" type="text/javascript"></script> 
    <script type="text/javascript"> 
vmSiteurl = 'http://localhost/offer_e_shop_1/' ; 
vmLang = "" 
vmCartText = ' was added to your cart.' ; 
vmCartError = 'There was an error while updating your cart.' ; 
loadingImage = '/offer_e_shop_1/components/com_virtuemart/assets/images/facebox/loading.gif' ; 
closeImage = '/offer_e_shop_1/components/com_virtuemart/assets/images/facebox/closelabel.png' ; 
Virtuemart.addtocart_popup = '0' ; 
faceboxHtml = '<div id="facebox" style="display:none;"><div class="popup"><div class="content"></div> <a href="#" class="close"></a></div></div>' ; 

    </script> 

<link rel="stylesheet" href="/offer_e_shop_1/templates/system/css/system.css" type="text/css" /> 
<link rel="stylesheet" href="/offer_e_shop_1/templates/system/css/general.css" type="text/css" /> 
<link rel="stylesheet" href="/offer_e_shop_1/templates/mov_jean_cc_offer_e_shop_2/css/print.css" type="text/css" /> 
</head> 
<body class="contentpane"> 

<div id="system-message-container"> 
</div> 
test 
</body> 
</html> 

如何仅返回测试值?

回答

1

您可以使用Jexit(),出口()或添加&格式=原在URL字符串或控制器功能使用给定的代码:

$app = &JFactory::getApplication(); 
$app->close() 
1

回显你所有的输出文本,只是放置一个die();之后

+0

PFF不工作..它仍然给我所有的头文件thingy – themis 2012-08-13 03:54:15

+1

你把哪里死();声明。 – Anand 2012-08-13 05:29:52

+0

函数参与控制器 – themis 2012-08-13 05:30:47

0

发现它..在URL字符串..你需要连接&格式的结束=原 因此请求将

jQuery.ajax({ 
       type: "POST", 
       url: "index.php?option=com_virtuemart&amp;view=participate&amp;task=participate_request&amp;virtuemart_product_id=2&amp;virtuemart_category_id=5&amp;tmpl=component&amp;Itemid=101&format=raw", 
       data: { name: "John", location: "Boston" } 
      }).done(function(msg) { 
       alert("Data Saved: " + msg); 
     }); 

!毕竟这么容易。

1

你只需要添加Jexit()在你的任务&结束,你将摆脱完整的HTML。你也可以使用die(),但为什么如果有Jexit()。 function edit() { //您的代码 Jexit(); }