簡體   English   中英

通過ajax發布數據時丟失參數錯誤(使用Laravel)

[英]Missing argument error when posting data through ajax (using Laravel)

我會精確地說:我在$.post jQuery函數和控制器中的操作中遇到了一個奇怪的問題。 我是PHP的新手,但是我以前使用過ASP.NET MVC,希望它們相似。 這是問題所在:

//This is my ajax call
$.post('get_random_photos', count, function(data) {
    ...
});

//This is my action
public function post_get_random_photos($count) {
    ...
}

//This is what I have in my routes.php
Route::post('photos/get_random_photos', 'photos@get_random_photos');

當我發出ajax請求時,收到以下消息:

Missing argument 1 for Photos_Controller::post_get_random_photos()

我知道這可能可以使用Input::get('count');解決Input::get('count'); 但我寧願具有count作為動作參數,為我所用的ASP.NET MVC做。 有什么辦法可以做到這一點?

您的靜態路由需要在url中接收該計數,這是一種實現方法:

//This is my ajax call
$.post('get_random_photos/'+count, 'nothing', function(data) {
    ...
});

//This is my action
public function random_photos($count) {
    ...
}

//This is what I have in my routes.php
Route::post('photos/get_random_photos/(:num)', 'photos@random_photos');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM