我爲購物車創建了一個PrestaShop模塊,因此我必須在創建將在其系統中工作的模塊時遵循規則集框架,並且我想通過AJAX提交表單無需重新加載頁面。在後臺使用AJAX PrestaShop模塊
這裏是一個建立並決定顯示哪些模塊頁面的修剪版本:
<?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();
?>
但我總是得到錯誤遺漏的類型錯誤:無法讀取屬性「響應「爲空,這顯然告訴我數據。響應沒有正確發送,因爲它不知道什麼是response
。
如果我手動測試頁面,一切工作正常,所以它導致我相信它或者可能在類中的事實可能嗎?而且我必須做一些與平時不同的事情才能讓它發送數據?
或者PrestaShop不允許模塊通過AJAX運行,但我可以在他們的網站上找到的唯一與此相關的事情是有人asking the same question in their forum,它沒有回覆/修復。
我還想指出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();
?>
當我提交頁面我得到的警報真實,如果我認爲我ajax.php得到{"response":1}
。所以代碼本身沒問題,只是將它與其類/函數集成在一起。
你正在使用哪個prestashop版本? –
PrestaShop v1.5.2.0 - 當前也在Windows 7(64位)上使用XAMPP v3.1.0.3.1.0在Chrome v23.0.1271.95m – Joe
上運行它如果您檢查homeslider模塊的代碼,您可能會了解有關ajax的信息模塊,這個模塊使用ajax來改變幻燈片的順序。 –