【问题标题】:Laravel and JQuery URL callLaravel 和 JQuery URL 调用
【发布时间】:2014-09-20 08:54:55
【问题描述】:

我正在尝试从个人资料网站中删除图片。上传工作正常,但如果我想删除 ajax 调用不能正常工作。我是一个 laravel 初学者,只是在玩。

我的头型是这样的:

    {{ $profileImageSection }}
<form
    name="update-account"
    action="{{ route('customer.account.profile.action') }}"
    accept-charset="UTF-8"
    enctype="multipart/form-data"
    class="with-padding with-validation"
    data-image-action="{{ route('customer.account.profile.image.action') }}"
    data-image-delete="{{ route('customer.account.profile.image.delete') }}">
    <input type="hidden" name="_token" value="{{ csrf_token() }}" />
    <input type="hidden" name="account_type" value="_Private" />
    <div class="holder message"></div>

我的 ProfileImageSection 是这样的:

    <section class="profile-image">
<div class="profileimage-round">
    @if (empty($image))
    <figure class="placeholder"><i></i></figure>
    @else
    <figure class="placeholder" style="background: url({{ $image }}) no-repeat"></figure>
    @endif
</div>
<form class="upload" name="upload-profile-image">
    <label>Drope here</label>
    <img src="{{ asset('assets/images/loader.gif') }}" />
    <a href="#" class="chose">Choose</a>
    <input type="file" name="profile" class="updatepic" style="/*display: block !important;*/ opacity: 0"/>
</form>
@if (!empty($image))
<form class="delete" name="delete-profile-image">
    <a href="#" class="delete">Delete</a>
    <input type="button" name="profile" class="deletepic" style="/*display: block !important;*/ opacity: 0"/>
</form>
@endif

我用 javascript 尝试的内容类似于:

$('.profile-image .delete a.delete').click(function()
    {
        $(this).parent().find('.deletepic').click();
    });

    $.('form[name=delete-profile-image]')({
        url: $('form[name=update-account]').data('image-delete'),
        type: 'POST',
        dataType: 'json'
    });

网址正确,我的路由如下:

    Route::post('/delete', array(
            'as'    => 'customer.account.profile.image.delete',
            'uses'  => 'UserProfileController@postImage'
        ));

但它不起作用。我尝试了很多,但找不到解决方案了。我认为路由没有调用。谁能帮帮我?

【问题讨论】:

  • 不,什么也没发生。

标签: javascript php jquery laravel url-routing


【解决方案1】:

我看到的几个问题..

如果您使用 form helpers ( Form::open() / Form::close() ),则无需手动包含隐藏的 CSRF 令牌元素。然而更大的问题是你的javascript:

$('.profile-image .delete a.delete').click(function() {
    $(this).parent().find('.deletepic').click();
});

$.('form[name=delete-profile-image]')({
    url: $('form[name=update-account]').data('image-delete'),
    type: 'POST',
    dataType: 'json'
});

AJAX 调用在哪里? (提示,它只是不存在..)当您可以在锚本身上触发 AJAX 调用时,为什么要让锚元素触发对表单内隐藏按钮的单击?尝试将您的 HTML 更改为这样的内容...

@if (!empty($image))
    <a href="#" class="delete">Delete</a>
    {{ Form::open(array('url' => route('customer.account.profile.image.delete'), 'style' => 'display: none', 'id' => 'image_delete')) }}
        {{ Form::text('image', $image) }}
    {{ Form::close() }}
@endif

并将您的 javascript 更新为如下内容:

$('.profile-image .delete a.delete').click(function() {
    $('#image_delete').submit()
});

【讨论】:

  • 哦,我明白了,你当然是对的!谢谢你这么快的回复。现在它正在路由到 /delete。我只想在 /delete 下使用 ajax 调用 UserProfileController@postImage。这样用户就不会离开当前站点。就像使用“Dropzone”上传的图片一样。 'action' => 'UserProfileController@postImage' 不起作用。现在我怎么称呼它但不离开网站?
猜你喜欢
  • 1970-01-01
  • 2013-06-09
  • 2019-03-19
  • 2015-12-14
  • 2016-12-03
  • 2016-07-11
  • 2017-06-06
  • 2012-02-27
  • 1970-01-01
相关资源
最近更新 更多