我是Joomla,Javascript和JQuery的新手。 我正在编写一个Joomla 3.x组件,并试图以后端形式使用Ajax调用。
我编写了一个Ajax调用来在edit.php中动态更新一些字段(参见下面的代码)。我附上了很大一部分代码,因为我想知道我是不是搞乱了javascript,Jquery和Joomla的东西......
我在运行时在chrome上遇到以下错误:Uncaught SyntaxError:行方法的意外标识符:" POST",我的Ajax调用。
任何帮助都将受到高度赞赏。 非常感谢你花在我的问题上的时间。
祝你好运
埃里克
<?php
/**
* @package Joomla.Administrator
* @subpackage com_emc
*
* @copyright Copyright (C) 2015 Eric LLANO. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
// No direct access
defined('_JEXEC') or die('Restricted access');
JHtml::_('jquery.framework');
JHtml::_('behavior.formvalidation');
JHtml::_('formbehavior.chosen', 'select');
JHTML::_('behavior.modal');
?>
<script type="text/javascript">
jQuery.noConflict();
function checkAmount() {
//billable_items_table = document.getElementById("#jform_billable_items_table_table");
console.log( "Table modified" );
//alert(billable_items_table.value);
}
function calculateAmount() {
billable_items_table = document.getElementById("jform_billable_items_table_table");
//
console.log( "Table saved" );
alert(billable_items_table.value);
}
function calculateVAT() {
console.log( "VAT modified" );
calculateVATAjax();
}
jQuery(document).ready(function($){
console.log( "ready!" );
function calculateVATAjax() {
var taxable = document.getElementById("jform_taxable");
var amount_exc_VAT = document.getElementById("jform_amount_exc_VAT");
jQuery.ajax({
method: "POST",
url: "index.php?option=com_emc&task=invoice.calculateVAT&format=json",
data: { taxable: taxable.value,
amount_exc_VAT: amount_exc_VAT.value,
<?php echo(JSession::getFormToken());?>: 1
},
success: function(r) {
if (!r.success && r.message) {
// Success flag is set to 'false' and main response message given
// So you can alert it or insert it into some HTML element
alert(r.message);
}
if (r.messages) {
// All the enqueued messages of the $app object can simple be
// rendered by the respective helper function of Joomla!
// They will automatically be displayed at the messages section of the template
Joomla.renderMessages(r.messages);
}
document.getElementById("jform_VAT_rate").value = r.data.VATrate;
document.getElementById("jform_VAT_amount").value = r.data.VAT_amount;
document.getElementById("jform_amount_inc_VAT").value = r.data.amount_inc_VAT;
//if (r.data)
//{
// Here you can access all the data of your response
//alert(r.data);
//}
},
failure: function(xhr) {
// Reaching this point means that the Ajax request itself was not successful
// So JResponseJson was never called
alert('Ajax error');
},
error: function(text, error) {
// Reaching this point means that the Ajax request was answered by the server, but
// the response was no valid JSON (this happens sometimes if there were PHP errors,
// warnings or notices during the development process of a new Ajax request).
alert("Ajax request was answered by the server"+"\n"+error + "\n" + text);
}
});
}
// Add the event handlers
$("#jform_taxable").change(calculateVAT);
$("#jform_billable_items_table_table").change(checkAmount);
$(".save-modal-data").click(calculateAmount);
});
</script>