jQuery / Ajax - 请求返回错误

时间:2017-07-03 09:45:11

标签: jquery ajax magento

我试图从下面的网址获取电子邮件,但是例如,如果我转到浏览器中的网址,它会返回{"customer_email": "email@email.com" },但下面的ajax请求总是返回错误 - 我正在做什么错?

var url = window.location.host + "/emarsys/ajax/getLoggedInCustomerEmail/?isAjax=1";

jQuery.ajax({
    url: url,
    dataType: 'json',
    type: 'GET',
    success: function (result) {
        console.log('success');
    },
    error: function () {
        console.log('error');
    }
});


class Emarsys_AjaxController extends Mage_Core_Controller_Front_Action {

public function getLoggedInCustomerEmailAction(){
    $response = ['customer_email' => ''];
    $customerSession = Mage::getSingleton('customer/session');
    if (Mage::app()->getRequest()->isAjax() && $customerSession->getCustomerId()) {
        $response['customer_email'] = Mage::getSingleton('customer/session')->getCustomer()->getEmail();
    }

    $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($response));
}
}

2 个答案:

答案 0 :(得分:0)

我检查了网络标签,它返回404,因为网址加倍,所以我将其更改为:url: "/emarsys/ajax/getLoggedInCustomerEmail/?isAjax=1",修复了它

答案 1 :(得分:0)

试试这个 var url = window.origin +" / emarsys / ajax / getLoggedInCustomerEmail /?isAjax = 1&#34 ;;