我收到这个json错误,ajax提交表单没有关闭

时间:2015-11-10 03:46:51

标签: javascript php ajax cakephp

这是我得到的json错误

SyntaxError:JSON.parse:JSON数据第1行第1列的意外字符

if(e.which == 9){

这是我的控制器方法

//在' Combos'的订单输入表单中添加结构和颜色数据产品类型..

    function add_ajax_combos($newFabricName = '', $fabricSavedCallback = '', $product_type_id = '', $field = '') {
        if (!empty($this->data)) {
            $this->autoRender = false;
            $this->layout = 'ajax';

            $name = $this->data['FabricRange']['name'];
            $product_id = $this->data['FabricRange']['product_id'];
            $chk_range_exists = $this->FabricRange->find('count', array('conditions' => array('FabricRange.name' => $name, 'FabricRange.product_id' => $product_id)));
            if ($chk_range_exists == 0) {
                $chk_same_name_already_exist = $this->FabricRange->find('first', array('conditions' => array('FabricRange.name' => $name), 'fields' => array('range_id')));
                $update_flag = false;
                if ($chk_same_name_already_exist) {
                    $this->data['FabricRange']['range_id'] = $chk_same_name_already_exist['FabricRange']['range_id'];
                } else {
                    $update_flag = true;
                }
                $name = $this->data['FabricRange']['colour'];
                $usable_width = $this->data['FabricRange']['usable_width'];
                $range_id= $this->data['FabricRange']['range_id'];

                $chk_range_color_exists = $this->FabricRangeColour->find('count', array('conditions' => array('FabricRangeColour.name' => $name, 'FabricRangeColour.usable_width' => $usable_width)));
            }

            if ($chk_range_color_exists == 0) {
            $usable_width = $this->data['FabricRange']['usable_width'];
                $purchase_wizard_code = $this->data['FabricRange']['purchase_wizard_code'];
                $can_turn = $this->data['FabricRange']['can_turn'];
                $pattern_repeat = $this->data['FabricRange']['pattern_repeat'];
                $deleted = $this->data['FabricRange']['deleted'];
                $discontinued = $this->data['FabricRange']['discontinued'];

                $ds = $this->FabricRange->getDataSource();
                $ds->begin($this->FabricRange);
                $this->FabricRange->create();
                if ($this->FabricRange->save($this->data)) {
                    if ($update_flag) {
                        $id = $this->FabricRange->id;   
                        $this->FabricRange->saveField('range_id', $id);
                        $fabric_ranges_id = $id;
                    } else {
                        $fabric_ranges_id = $this->data['FabricRange']['range_id'];
                    }
                    $fabricColourId = 0;
                    if (isset($this->data['FabricRange']['colour_id']) == false || strlen(trim($this->data['FabricRange']['colour_id'])) == 0) {
                        // new colour
                        $this->FabricRangeColour->create();
                        if ($this->FabricRangeColour->save(array('FabricRangeColour' => array('name' => $name, 'fabric_ranges_id' => $fabric_ranges_id, 'usable_width' => $usable_width, 'purchase_wizard_code' => $purchase_wizard_code
                                        , 'can_turn' => $can_turn, 'pattern_repeat' => $pattern_repeat, 'deleted' => $deleted, 'discontinued' => $discontinued))) == false) {
                            $ds->rollback($this->FabricRange);
                            return json_encode(array("status" => "FAILURE"));
                        }
                        $fabricColourId = $this->FabricRangeColour->id;
                    } else {
                        $fabricColourId = $this->data['FabricRange']['colour_id'];
                    }

                    $ds->commit($this->FabricRange);
                    return json_encode(array("status" => "SUCCESS", 'fabric' => $this->data['FabricRange']['name'], 'id' => $this->FabricRange->id, 'colour_id' => $fabricColourId, 'colourname' => $this->data['FabricRange']['colour'], 'fabric_category_id' => $this->data['FabricRange']['fabric_categories_id']));
                } else {
                    $ds->rollback($this->FabricRange);
                    return json_encode(array("status" => "FAILURE"));
                }
            } else {
                return json_encode(array("status" => "FAILURE"));
            }
        }
        $this->layout = 'ajax';
        $fabricCategories = $this->FabricCategory->getFabricCategoryList('list');
        $fabricColours = $this->FabricRangeColour->getAllAsList();
        $this->set('fabric_name', $newFabricName);
        $result_pr = $this->ProductType->find('first', array(
            'fields' => array('product_id'), 'conditions' => array('id' => $product_type_id)
        ));
        $product_id_title = $result_pr['ProductType']['product_id'];
        $this->set('product_id', $product_id_title);
        $this->set('field', $field);
        $this->set(compact('fabricCategories', 'fabricColours', 'fabricSavedCallback'));


}

这是我的ctp

function ajaxSubmit(field) {
        var requestUrl = '<?php echo $this->Html->url(array('controller'=>'fabric_ranges','action'=>'add_ajax_combos'),true);?>';
        if (validateForm()) {
            $.post(requestUrl, $('#FabricRangeAddAjaxCombosForm').serialize(),
                    function(data) {
                        data = JSON.parse(data);
                        console.log(data);
                        if (data.status == 'SUCCESS') {
                            if (typeof <?php echo $fabricSavedCallback;?> != 'undefined') {
                                   <?php echo $fabricSavedCallback;?>(data.id, data.fabric, data.colour_id, data.colourname, data.fabric_category_id);
                                if (field == 1) {
                                    $("#fabricsAutoComplete").val(data.fabric);
                                    $("#OrderEntryItemRollerBlindComboFabricFrontBlind").val(data.id);
                                    $("#OrderEntryItemRollerBlindComboColours").val(data.colourname);
                                    $("#OrderEntryItemRollerBlindComboFabricColourFrontBlind").val(data.colour_id);
                                    $("#OrderEntryItemRollerBlindComboFabricCategoriesId").val(data.fabric_category_id);
                                }
                                else {

                                    $("#fabricsAutoCompleteBack").val(data.fabric);
                                    $("#OrderEntryItemRollerBlindComboFabricBackBlind").val(data.id);
                                    $("#OrderEntryItemRollerBlindComboColoursBack").val(data.colourname);
                                    $("#OrderEntryItemRollerBlindComboFabricColourBackBlind").val(data.colour_id);
                                    $("#OrderEntryItemRollerBlindComboFabricCategoriesId2").val(data.fabric_category_id);
                                }


                            }
                        } else {
                            alert('An error occured while adding the fabric. The new fabric wasn\'t saved.');
                        }
                    }
            );
        }
    }

这是调用closeandupdatefabrics

的方法
function closeAndUpdateFabrics(fabricId, fabric,color, colourId) {
                       $('#addFabricDialog').dialog('close');
                        $("#fabricsAutoComplete").val(fabric);
                        $("#OrderEntryItemRollerBlindComboFabricFrontBlind").val(fabricId);
                        $("#OrderEntryItemRollerBlindComboColours").val(color);
                       var fabricsIdBox = document.getElementById('OrderEntryItemRollerBlindComboFabricFrontBlind');
                       setFabriCategory(fabricsIdBox);
                       loadFabricColours(fabricsIdBox, 'OrderEntryItemRollerBlindComboFabricColourFrontBlind', function() {
                           $("#OrderEntryItemRollerBlindComboFabricColourFrontBlind").val(colourId);
                      });
                    } 

我的问题是ajax框没有关闭,在控制台中给我一个json错误。

0 个答案:

没有答案