How to return a single value from a controler afte

2019-08-11 04:30发布

问题:

I am making an ajax call to a controler from a view like this

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 );
        });

and in the controler I have a task method declared like this

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

And I get this response

<!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>

How can I return only the test value alone?

回答1:

You can use Jexit(),exit() or add &format=raw in url string or use given code in controller function:

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


回答2:

echo all your output text and just place a die(); after that



回答3:

You just have to add Jexit() at the end of your task & you will get rid of full html. You can use die() as well but why if there is Jexit(). function edit() { // Your code Jexit(); }



回答4:

found it.. in the end of the url string.. you need to attach &format=raw So the request will be

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 );
        });

! so easy after all.