Silex: jQuery AJAX request throws exception - No route found for “GET …”

*爱你&永不变心* 提交于 2019-12-11 19:54:08

问题


I'm trying to make an ajax call on tr click like this:

$.ajax({
    type: "GET",
    url: "/segments/ajaxGetHostsSegment",
    data: {
        deelgebied: deelgebiedid
    },
    success: function( data ) {
        // CHECK ID'S WITH ID'S IN FORM AND CHECK CHECKBOXES
    },
    error: function (xhr, ajaxOptions, thrownError) {
        console.log(xhr.status);
        console.log(thrownError);
    }
})

In my routes file I have:

$app->get('/segments/ajaxgethostssegment', 'Segments\Controller\IndexController::ajaxGetHostsSegment')->bind('segments.gethosts');

My Controller action:

public function ajaxGetHostsSegment(Application $app, Request $request)
{
    $deelgebied = $request->request->get('data');
    var_dump($deelgebied);
    die();
}

But I always get the error:

No route found for "GET /segments/ajaxGetHostsSegment" (from "http://mext-pst.localhost:8080/segments/view/PSS1400023")

回答1:


wrong http request method:

You're sending a GET request instead of a POST request with jQuery.

$.ajax() performs a GET request by default.

Silex throws the exception because you have not defined a route for GET but only for POST.

$app->post('..')

solution: add the request-type to $.ajax

$.ajax({
    type: "POST",
    // ...
});


来源:https://stackoverflow.com/questions/22011254/silex-jquery-ajax-request-throws-exception-no-route-found-for-get

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!