[英]Using AJAX in a backoffice PrestaShop module
我正在為購物車PrestaShop創建模塊,因此在創建將在其系統中使用的模塊時必須遵循一套規則框架,並且我想通過AJAX提交表單而不重新加載頁面。
這是模塊頁面的精簡版本,可構建並確定顯示的內容:
<?php
class mymodule extends Module
{
private $_html = '';
// Module information
function __construct()
{
// Get shop version
$versionMask = explode('.', _PS_VERSION_, 3);
$versionTest = $versionMask[0] > 0 && $versionMask[1] > 3;
// Module info
$this->name = 'MyModule';
$this->tab = $versionTest ? 'administration' : 'Administration';
if ($versionTest) { $this->author = 'JD'; }
$this->version = '0';
parent::__construct();
$this->displayName = $this->l('MyModule');
$this->description = $this->l('Description...');
}
// Display content
public function getContent()
{
$this->_displayForm();
return $this->_html;
}
// Build the display
private function _displayForm()
{
$this->_html .= '<script src="../modules/mymodule/scripts.js" type="text/javascript"></script>
<form name="formName" id="formName" method="get">
<input type="submit" name="submitModule" value="Continue" />
</form>';
}
}
?>
通常,有一個私有的_postProcess
函數處理表單數據,然后在頁面重載時調用getContent
函數,然后您可以檢查該函數是否應顯示表單或結果等。
但是由於我想使用AJAX進行此操作,因此我刪除了_postProcess
函數,該函數鏈接到具有以下內容的scripts.js
:
$(document).ready(function() {
$('#formName').submit(function(e)
{
e.preventDefault();
$.ajax({
url: "ajax.php",
type: "GET",
dataType: "json",
success: function(data)
{
if (data.response == 1)
{
alert('true');
}
else
{
alert('false');
}
}
});
});
});
還有我已經真正修剪過的ajax.php文件本身,因此不得不顯示結果:
<?php
$json['response'] = 1;
echo json_encode($json);
exit();
?>
但我總是收到錯誤Uncaught TypeError:無法讀取null的屬性'response' ,這顯然是在告訴我data.response無法正確發送,因為它不知道response
是什么。
如果我手動測試頁面,那么一切都會正常運行,因此使我相信它可能與某個類有關嗎? 而且我必須做一些與平常不同的事情才能使其通過數據發送?
或PrestaShop不允許模塊通過AJAX運行,但是我在他們的網站上唯一與此相關的是有人在他們的論壇中問同樣的問題,並且沒有回復/修復。
我還想指出AJAX在某種程度上起作用了,如果在成功函數中放了alert("hello");
顯示警報彈出窗口。
有人對我可能會出錯的地方有任何想法嗎?
Uncaught TypeError: Cannot read property 'response' of null scripts.js:132
$.ajax.success scripts.js:132
o jquery-1.7.2.min.js:2
p.fireWith jquery-1.7.2.min.js:2
w jquery-1.7.2.min.js:4
d
scripts.js:132
引用以下行: if (data.response == 1)
另外,我已經將其從類中取出並放在普通的頁面/單獨目錄中,並且具有相同的代碼,只是不在類/函數內部:
的index.php
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="scripts.js" type="text/javascript"></script>
<form name="formName" id="formName" method="get">
<input type="submit" name="submitModule" value="Continue" />
</form>
scripts.js
$(document).ready(function() {
$('#formName').submit(function(e)
{
e.preventDefault();
$.ajax({
url: "ajax.php",
type: "GET",
dataType: "json",
success: function(data)
{
if (data.response == 1)
{
alert('true');
}
else
{
alert('false');
}
}
});
});
});
ajax.php
<?php
$json['response'] = 1;
echo json_encode($json);
exit();
?>
當我提交頁面時,我得到的警報為true,如果我查看ajax.php,則會得到{"response":1}
。 這樣代碼本身就可以了,只需將其與類/函數集成在一起即可。
似乎是scripts.js文件中ajax.php文件的路徑引起了問題。
這與prestashop中文件夾的結構有關,所有模塊都放在/ prestashop / modules /目錄中,但可以從/ prestashop / admin /中查看這些模塊。 因此,將它們更改為完整路徑可以解決此問題。
多虧了在這里提供幫助的人們,它仍然受到贊賞!
我認為您的結果發送正常,但未解釋為JSON,這是jquery的錯誤-我敢說。
header('Content-Type: text/javascript; charset=utf8');
將其放入PHP腳本應該可以解決問題,並強制正確解釋json數據。 如果先前的建議沒有幫助您,請嘗試使用$ .parseJson();。 如jQuery文檔中所示:
var obj = jQuery.parseJSON('{"name":"John"}');
alert( obj.name === "John" );
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.