如何将带有图像src的ajaxpost发送到zend控制器并将其保存在数据库中,而不使用发送表单

如何将带有图像src的ajaxpost发送到zend控制器并将其保存在数据库中,而不使用发送表单,ajax,view,model,controller,zend-framework2,Ajax,View,Model,Controller,Zend Framework2,我想知道如何从视图到zend controller的ajax调用中获取数据,并使用zend表函数保存数据。下面是我的代码 这是我的控制器 public function addAction(){ $image_uri = $this->getRequest()->getPost('image_uri'); $personalmug = new PersonalMug(); $personalmug->exchangeArray($im

我想知道如何从视图到zend controller的ajax调用中获取数据,并使用zend表函数保存数据。下面是我的代码

这是我的控制器

    public function addAction(){    

    $image_uri =  $this->getRequest()->getPost('image_uri');
    $personalmug = new PersonalMug();
    $personalmug->exchangeArray($image_uri);
    $this->getPersonalMugTable()->save($personalmug);



}
这是我的ajax代码

    $('#buttonSave').click(function (){
        var image_uri = $("mug1").attr("src");
        $.ajax({
                type: 'POST',
                dataType: 'json',
                url: 'http://toxicfox.com/personal-mug/add',
                async: false,

                // you can use an object here
                data: { image_uri: image_uri},
                success: function(json) {
                    console.log(json.image_uri);
                }
            });
        // you might need to do this, to prevent anchors from following
        // or form controls from submitting

    });
这是zend table类中的save函数

    public function save(PersonalMug $personalmug){
    $data = array(
        'image_uri'  => $personalmug->image_uri,
    );

    $image_id = (int) $$personalmug->image_id;
    if($image_id == 0){
        $this->tableGateway->insert($data);
    }else{
        if($this->getImage($image_id)){
            $this->tableGateway->update($data, array('image_id' => $image_id));
        }else{
            throw new \Exception('Mug id does not exist');
        }
    }
}
这是我的模型课

class PersonalMug{
public $image_id;

public $image_uri;

public function exchangeArray($data){
    $this->image_id = (!empty($data['image_id'])) ? $data['image_id'] : null;
    $this->image_uri  = (!empty($data['image_uri'])) ? $data['image_uri'] : null;
}

}

我找到了解决方案,我犯了一些小错误,我插入的是字符串而不是对象。我希望它也能帮助其他人。下面是控制器代码,纠正了一些小错误,现在可以正常工作了。 错误

我在PersonalMug表中使用了reference变量

$image\u id=int$$personalmug->image\u id

我没有在jquery中使用sybmol

var image_uri=$mug1.attrsrc

我在控制器中插入字符串而不是对象

公共函数addAction{

$data['image\u uri']=$this->getRequest->getPost'image\u uri'; $personalmug=新的personalmug; $personalmug->exchangeArray$数据; $this->getPersonalMugTable->保存$personalmug; 返回$this->redirect->toRoute'personal-mug'

}


除了这个,一切看起来都很好:$image\u id=int$$personalmug->image\u id;它应该是:$image\u id=int$personalmug->image\u id;你有任何错误吗?在jquery函数中获取图像src存在问题,它总是发送null post,你是正确的,我必须删除双美元符号,但现在我得到的错误语句无法执行23000-1048-Le champ'image_uri'ne peutêtre vide nulls,并且此错误也是SQLSTATE[23000]:完整性约束冲突:1048 Le champ'image_uri'ne peutêtre vide null,我是否也必须发送图像的id,但id在放回后自动生成我是指美元符号我有此错误可捕获致命错误:类PersonalMug\Model\PersonalMug的对象无法转换为字符串