当我在控制器函数内部回显某些内容时,会显示jquery错误

时间:2016-05-23 10:43:11

标签: php jquery laravel

任何人都可以帮助我:

这是我的控制器功能:

function postShareNetwork(){

            $input   = Input::all();
            $userIds =  $input['users'];
            $sender = $this->contRepo->getUserByID($this->getCurrentUser());
            $subscriber = array();
            $filter['notifyon_share_folder'] = 1;
            $receievers = $this->contRepo->getContactByEmailSetting((array)$userIds, $filter);

            $msg = "Document has been shared successfully.";
            if(!empty($input['folder'])) {
                $msg = "Folder has been shared successfully.";
            }

            if(empty($receievers)) {
                return Response::json([
                    'success' => true,
                    'errors' => array($msg)
                ]);
            }

            $folder = \Resource::find($input['folder']);


            if(!empty($input['folder'])){
                $resourceId = $folder->id;
                $view = 'network::emails.folder-emailed';
                $folderName = $folder->display_name;
            }elseif ( !empty($input['doc'])) {
                $doc = $this->resRepo->getFile($input['doc'])[0];
                $resourceId = $doc->id;
                $fileName = $doc->display_name;
                $view = 'network::emails.shared-network-doc';
                $subject = 'file shared';
            }

            $actAfter =  [
                \TempAction::TYPE_ADD_COLLEAGUE => [
                    'act_type' => 'after_login',
                    'args' => [
                        'sender' => $this->getCurrentUser()
                    ]
                ],
                \TempAction::TYPE_NETWORK_SHARE => [

                    'act_type' => 'after_login',
                    'args' => [
                        'sender' => $this->getCurrentUser(),
                        'resourceId' => $resourceId
                    ]
                ]
            ];

            foreach ($receievers as $user) {
                /*$token = Crypt::encrypt($resourceId.'_'.$user->id.'_'.$this->getCurrentUser());*/

                $actParams = [
                    'receiver'=> $user->email,
                    'after'   => $actAfter
                ];

                $jsonEncoded = json_encode($actParams);
                $linkToken = Crypt::encrypt($jsonEncoded);

                //\URL::route('shared-network-resource', $token);
                $link  = route('temp-email-link', ['token' => $linkToken]);
                if ( !empty($input['doc'])) {
                    $subject = $fileName .' Shared by: '.$sender[0]->getName();
                }else{
                    $subject = $user->getName() . ' Join My KShare Network';
                }

                $data = [
                    'email'       => $user->email,
                    'subject'     => $subject,
                    'name'        => $user->getName(),
                    'sender_f_name' => $sender[0]->profile->first_name,
                    'sender_l_name' => $sender[0]->profile->last_name,
                    'sender_name' => $sender[0]->getName(),
                    'sender_email'=> $sender[0]->email,
                    'share_link'  => $link,
                    'email_message' => $subject,
                    'file_name'     => isset($fileName) ? $fileName : null,
                    'folder_name' => isset($folderName) ? $folderName : null
                ];

                \Mail::queue($view, $data, function($messages) use ($data){ 
                    $messages->to($data['email'], $data['name'])
                        ->subject($data['subject'] );
                });

            }

            return Response::json([
                'success' => true,
                'errors' => array($msg)
            ]);

    }

表单提交时的jquery如下:

$modelPopup.find('#form-add-email').on('submit', function(){

        var $form = $(this),
        $selUsers = $listUsers.filter(':checked');

        // validate form to select at least one user.
        if($selUsers.length == 0){
            alert("Please select at least one user");
            return false;
        }

        // post data.
        $.ajax({
            url: $form.attr('action'),
            type: "POST",
            data: $(this).serialize(),
            dataType: "json",
            beforeSend: function() {
                $('#loading').show();
            },
            error: function(jq,status,message) {
                $('#loading').hide();
                $msg ="<p class='msg msg_unsuccess'>"+ 'A jQuery error has occurred. Status: ' + status + ' - Message: ' + message + '</p>';
                $('.msg_wrapper').html($msg);
                $('.msg_wrapper p.msg').delay(3000).fadeOut();
            }
        }).done(function(response) {
            $('#loading').hide();
            if (response.success==true) {
                var msg = "<p class='msg msg_success'>"+response.errors+"</p>";
                $modelPopup.find('.close').click();
                $('.msg_wrapper').html(msg);
            }
            if (response.success==false) {
                var msg = "<p class='msg msg_unsuccess'>"+response.errors+"</p>";
                $('#share_via_mail .msg_wrapper').html(msg);
                $('.msg_wrapper').html(msg);
            }
            $('.msg_wrapper p.msg').delay(3000).fadeOut();
            $('.btn').removeClass('no_loader');
                $('.reset_loader').addClass('no_loader');
        });

        return false;
    });

问题在于,当我简单地回显名称或任何文本或在函数内部做任何事情时,它会抛出jquery错误,所以我没有找到我错的地方请帮我解决这个问题

1 个答案:

答案 0 :(得分:1)

您期望服务器有效的JSON(如{"name":"Some Name"}

现在当你回应某些内容时,它会变成这样的Echoed text{"name":"Some Name"}或只是Echoed Text,而这是无效的JSON导致Ajax请求失败。

您可以在测试和回复时删除dataType: "json"

无论哪种方式,在控制台网络选项卡中,您始终都会看到服务器响应(即使JSON无效)