dqkf49487 2017-12-10 02:33
浏览 44
已采纳

如何在Laravel中编辑部分AJAX数据返回?

I have an AJAX request (through a GET method) using the following code:

<script>
    $(document).ready(function(){
    $('.attachmentDetailsButton').click(function(){
            var $a = $(this);
                $.ajax({
                type:'GET',
                url: '/shipments/attachmentDetails',
                dataType:'json',
                data: {
                    'attachmentID': $a.data('id'),
                   '_token': $('input[name=_token]').val(),
                },


                  success: function(data) {
                        $('.errorTitle').addClass('hidden');
                        $('.errorContent').addClass('hidden');

                        if ((data.errors)) {
                            setTimeout(function () {
                                $('#createOrigin').modal('show');
                                toastr.error('Check your inputs!', 'Error Alert', {timeOut: 5000});
                            }, 500);

                            if (data.errors.title) {
                                $('.errorTitle').removeClass('hidden');
                                $('.errorTitle').text(data.errors.title);
                            }
                            if (data.errors.content) {
                                $('.errorContent').removeClass('hidden');
                                $('.errorContent').text(data.errors.content);
                            }
                        } else {
                            //toastr.success('Changed Bill To '+data.customer_name, 'Success Alert', {timeOut: 5000});

                            $('#attachmentIDdetails').html(
                              '<div id="viewer"><object data="http://www.pdf995.com/samples/pdf.pdf" type="application/pdf" width="100%" height="100%"><iframe src="http://www.pdf995.com/samples/pdf.pdf" style="border: none;" width="100%" height="100%">This browser does not support PDFs. Please download the PDF to view it: <a href="http://www.pdf995.com/samples/pdf.pdf">Download PDF</a></iframe></object></div><div id="details"><h3><strong>'+data.attachmentTitle+'</strong></h3><br>Description: '+data.attachmentBody+'<br>Date '+data.attachmentDate +'<br>Type: '+data.attachmentType +'<br>Visibility: '+data.attachmentVisibility + '<br>Uploaded by: '+data.user_id+'<br><a href="{{Storage::url('+data.attachmentPath+')}}">Download Attachment</a><br><img width="100%" src="https://maps.googleapis.com/maps/api/staticmap?center='+data.attachmentLat+',+'+data.attachmentLong+'&zoom=13&scale=1&size=600x300&maptype=roadmap&key=AIzaSyCPeq-OWJciDjoY8nSMsZeRu9IG9MDMwBM&format=png&visual_refresh=true&markers=size:mid%7Ccolor:0xff0000%7Clabel:%7C'+data.attachmentLat+',+'+data.attachmentLong+'" alt="Google Map of '+data.attachmentLat+',+'+data.attachmentLong+'"></div><div id="clear" style="clear: both;"></div>');
                            $('.modal-footer').html(
                            '<button type="button" class="btn btn-default" data-dismiss="modal">Close</button><a href="/files/i/'+data.url_string+'" target="_blank"><button type="button" class="btn btn-success"><span id="" class="glyphicon glyphicon-check"></span> View More Details</button></a>'
                            );
                        }
                    },

                    error: function (jqXHR, textStatus, errorThrown) {
                      if (jqXHR.status == 500) {
                          alert('Internal error: ' + jqXHR.responseText);
                      } else {
                          alert('Unexpected error.');
                      }
                    }
                });
            });
});
</script>

As you can see a little lower down in my script, you can see where the returned data is placed in a div called "attachmentIDdetails" - well one specific field it returns "data.attachmentPath" I need to have a word on the beginning removed. Every single time it returns "/public/storage/etc.etc.", and I need it to only remove the "/public" portion. This data is coming from a MySQL database, and there is a reason to add the "/public" in there in the beginning, so that can't change, so I'm hoping I can remove the data outright in the response.

  • 写回答

3条回答 默认 最新

  • dongwei1263 2017-12-10 02:54
    关注

    I have added the substring in the correct place for you, change this line

    '<div id="viewer"><object data="http://www.pdf995.com/samples/pdf.pdf" type="application/pdf" width="100%" height="100%"><iframe src="http://www.pdf995.com/samples/pdf.pdf" style="border: none;" width="100%" height="100%">This browser does not support PDFs. Please download the PDF to view it: <a href="http://www.pdf995.com/samples/pdf.pdf">Download PDF</a></iframe></object></div><div id="details"><h3><strong>'+data.attachmentTitle+'</strong></h3><br>Description: '+data.attachmentBody+'<br>Date '+data.attachmentDate +'<br>Type: '+data.attachmentType +'<br>Visibility: '+data.attachmentVisibility + '<br>Uploaded by: '+data.user_id+'<br><a href="{{Storage::url('+data.attachmentPath+')}}">Download Attachment</a><br><img width="100%" src="https://maps.googleapis.com/maps/api/staticmap?center='+data.attachmentLat+',+'+data.attachmentLong+'&zoom=13&scale=1&size=600x300&maptype=roadmap&key=AIzaSyCPeq-OWJciDjoY8nSMsZeRu9IG9MDMwBM&format=png&visual_refresh=true&markers=size:mid%7Ccolor:0xff0000%7Clabel:%7C'+data.attachmentLat+',+'+data.attachmentLong+'" alt="Google Map of '+data.attachmentLat+',+'+data.attachmentLong+'"></div><div id="clear" style="clear: both;"></div>');
    

    to

    '<div id="viewer"><object data="http://www.pdf995.com/samples/pdf.pdf" type="application/pdf" width="100%" height="100%"><iframe src="http://www.pdf995.com/samples/pdf.pdf" style="border: none;" width="100%" height="100%">This browser does not support PDFs. Please download the PDF to view it: <a href="http://www.pdf995.com/samples/pdf.pdf">Download PDF</a></iframe></object></div><div id="details"><h3><strong>'+data.attachmentTitle+'</strong></h3><br>Description: '+data.attachmentBody+'<br>Date '+data.attachmentDate +'<br>Type: '+data.attachmentType +'<br>Visibility: '+data.attachmentVisibility + '<br>Uploaded by: '+data.user_id+'<br><a href="{{Storage::url('+data.attachmentPath.substring(7);+')}}">Download Attachment</a><br><img width="100%" src="https://maps.googleapis.com/maps/api/staticmap?center='+data.attachmentLat+',+'+data.attachmentLong+'&zoom=13&scale=1&size=600x300&maptype=roadmap&key=AIzaSyCPeq-OWJciDjoY8nSMsZeRu9IG9MDMwBM&format=png&visual_refresh=true&markers=size:mid%7Ccolor:0xff0000%7Clabel:%7C'+data.attachmentLat+',+'+data.attachmentLong+'" alt="Google Map of '+data.attachmentLat+',+'+data.attachmentLong+'"></div><div id="clear" style="clear: both;"></div>');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵