简体   繁体   English

Ajax 忽略 URL

[英]Ajax ignoring URL

I try to make an ajax request through JQuery triggering an onClick event, but when it sends the AJAX request I receive:我尝试通过触发 onClick 事件的 JQuery 发出 ajax 请求,但是当它发送 ZA34A1659BCEAEAB7779E2 请求时

PATCH http://localhost:8000/courses 405 (Method Not Allowed) (Current page) Because it doesn't get the URL with the id PATCH http://localhost:8000/courses 405(不允许的方法) (当前页面)因为它没有得到带有 id 的 URL

HTML HTML

@foreach ($courses as $course)
    <tr>
      <td>{{ Form::select('year', $years, ['class' => 'form-control'], [ 'placeholder' => $course->academicYear]) }}</td>
      <td>{{ Form::select('subject', $subjects, ['class' => 'form-control'], [ 'placeholder' => $course->subject]) }}</td>
      <td>
        <a href="" id="saveCourse" class="btn btn-success pull-left">Save</a>
        <input type="hidden" id="idCourse" value="{{ $course->id }}">
      (...)

JQUERY + AJAX JQUERY + AJAX

$('#saveCourse').click(function(e){
        e.preventDefault();
        var id = $('#idCourse').val();

        // Ignore this logic
        var values = {year: "", subject:"", id: id};
        var parameters = ['year', 'subject'];
        var i = 0;
        $('td > select option:selected').each(function() {
            values[parameters[i]] = $(this).text();
            i++;
        });

        // Ajax request
        $.ajax({
            type: 'patch',

            // Appending the course id here not working, 
            // but if i put anything else like /blabla/ + id ajax doesn't ignore it...

            url:  '/courses/' + id,
            headers: {'X-CSRF-Token': csrf_token},
            dataType: 'json',
            data: values,

            success: function (response) {
                console.log("SUCCESS: " + response);
            },
            error: function (reject) {
                if( reject.status === 422 ) {
                    $("#error").text("Los datos dados no cumplen el formato requerido.");
                }
            }
        });
    });

WEB.PHP WEB.PHP

/* -----COURSE_ROUTES------ */
    Route::resource('courses', 'CourseController')->except([
        'create', 'edit'
    ]);

ROUTES路线路线

EDIT编辑

If I use POST instead of PATCH in type AJAX gets the id.如果我在 AJAX type中使用POST而不是PATCH获取 id。

Found a GitHub issue with the same problem https://github.com/jquery/jquery/issues/3944发现一个 GitHub 问题与同样的问题https://github.com/jquery/jquery/issues/3944

PUT and PATCH are request methods. PUT 和 PATCH 是请求方法。 An HTTP error of 405, which you get means that the server knows the request method, but the service does not support it. HTTP 错误 405,你得到的意思是服务器知道请求方法,但服务不支持。 Read more here: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/405在此处阅读更多信息: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/405

not sure if matters but try PATCH instead of patch不确定是否重要,但尝试 PATCH 而不是 patch

type: 'PATCH',

As mentioned below the 405 (METHOD NOT ALLOWED) basically means you're ajax request method PATCH is not allowed for the specific resource on the server.如下所述, 405 (不允许的方法)基本上意味着您是 ajax 请求方法PATCH不允许用于服务器上的特定资源。

If you using a routing library you can go their docs and search how to change this behavior.如果您使用路由库,您可以 go 他们的文档并搜索如何更改此行为。 A route can accept one or multiple request methods, I assume that the Route::resource create a route with method POST by default, which explains that the ajax request works in POST type.一个路由可以接受一个或多个请求方法,我假设Route::resource默认使用POST方法创建路由,这就解释了 ajax 请求在 POST 类型下工作。

I forgot to put this condition at the start of the update method in the controller... Now it works!我忘了把这个条件放在 controller 中更新方法的开头......现在它可以工作了!

if(request()->ajax()) { ... }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM