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条)

报告相同问题?

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装