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

时间:2017-12-10 02:33:48

标签: php jquery mysql ajax laravel

我有一个AJAX请求(通过GET方法)使用以下代码:

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

正如你在我的脚本中看到的那样,你可以看到返回数据放在名为“attachmentIDdetails”的div中的位置 - 以及它返回的一个特定字段“data.attachmentPath”我需要有一个单词开始删除。每次返回“/public/storage/etc.etc。”,我需要它只删除“/ public”部分。这些数据来自MySQL数据库,并且有一个理由在开头添加“/ public”,因此无法更改,因此我希望我可以在响应中直接删除数据。 / p>

3 个答案:

答案 0 :(得分:1)

我已将子字符串添加到正确的位置,更改此行

'<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>');

'<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>');

答案 1 :(得分:0)

我建议使用regex

此正则表达式删除路径名[/^\/[\w\d]+\//]

的第一部分

&#13;
&#13;
var data = "/public/storage/etc.etc.";
data = data.replace(/^\/[\w\d]+\//, '');
console.log(data);
&#13;
&#13;
&#13;

答案 2 :(得分:0)

使用String#replace()

重置该属性
data.attachmentPath = data.attachmentPath.replace('/public','')