我的Ajax电话。总是返回错误

时间:2016-03-30 11:10:25

标签: javascript php json ajax magento

大家好,我在magento工作,我搜索了很多,但没有得到任何解决方案,这是我的ajax

var $wk_jq = jQuery.noConflict();
  $wk_jq.ajax({
                url : "<?php echo $this->getUrl('mpmassuploadaddons/index/runProfiler'); ?>",
                type : 'GET',
                data : {id : profilerId,row : countOfStartedProfiles},
                dataType : 'json',
                success : function(content) {
                    console.log('content success');

                },
                 error: function(xhr, textStatus, errorThrown) {
                            console.log(xhr);
                            console.log(textStatus);
                            console.log(errorThrown);
                        }
            });

我总是得到parsererror

SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
responseText  "NULL\nNULL\n{"savedRows":1...":"successfully saved"}"
status  200
statusText  "OK"

即使我的PHP代码工作正常并返回json编码字符串{"savedRows":1,"success":"successfully saved"}这里有成功消息是我的PHP代码

public function runProfilerAction() {
    ob_start();
    $profileId = $this->getRequest()->getParam('id');
    $rowCount = $this->getRequest()->getParam('row');
    $profile = Mage::getModel('mpmassuploadaddons/profilesession')->load($profileId);
    if($profile->getproductType() == "simple") {
        $result = Mage::getModel('mpmassuploadaddons/mpmassupload')->importSimpleProducts($profileId,$rowCount);    
    } elseif($profile->getproductType() == "downloadable") {
        $result = Mage::getModel('mpmassuploadaddons/mpmassupload')->importDownloadableProducts($profileId,$rowCount);  
    } elseif($profile->getproductType() == "virtual") {
        $result = Mage::getModel('mpmassuploadaddons/mpmassupload')->importVirtualProducts($profileId,$rowCount);   
    } elseif($profile->getproductType() == "configurable") {
        $result = Mage::getModel('mpmassuploadaddons/mpmassupload')->importConfigurableProducts($profileId,$rowCount);   
    } 

    echo json_encode($result);
}

此json {"savedRows":1,"success":"successfully saved"}的格式也正确我在这里解码http://json.parser.online.fr/

及其罚款请帮助谢谢

1 个答案:

答案 0 :(得分:1)

添加此

ob_clean();

之前json_encode

希望它有效......