我正在将dom元素的id
作为ajax
数据发送到laravel控制器,但是Request
对象在控制器端为空。我已将自定义数组返回到ajax
,以检查控制器功能是否正常工作,并返回对ajax
成功的响应。
同一控制器上的其他功能从Request
获取ajax
数据,但不是此功能。我没有收到任何错误,因此很难调试问题
我的Ajax呼叫
$(document).on('click', 'i[id^="delete"]', function () {
var alias_id = $(this).prop('id');
//console.log(alias_id);
$.ajax({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
url: "{{ route('deletealiasname') }}",
method: "post",
data: {'alias_id': alias_id},
dataType: "json",
contentType: false,
cache: false,
processData: false,
beforeSend : function()
{
console.log(alias_id);
},
success: function (response) {
console.log(response);
}
});
});
路线:
Route::post('/deletealiasname', 'API\UserProfileController@deleteAliasName')->name('deletealiasname');
控制器:
public function deleteAliasName(Request $request)
{
$var = $request->all();
return array($var);
}
dd($ request)的输出
POST http://127.0.0.1:8000/api/deletealiasname
----------------------------------------------
------------ ----------------------------------------------------
date Thu, 06 Jun 2019 17:52:53 +0000
controller "UserProfileController"
source UserProfileController.php on line 165
file app\Http\Controllers\API\UserProfileController.php
------------ ----------------------------------------------------
Illuminate\Http\Request {#43
#json: null
#convertedFiles: null
#userResolver: Closure($guard = null) {#282
class: "Illuminate\Auth\AuthServiceProvider"
this: Illuminate\Auth\AuthServiceProvider {#40 …}
parameters: {
$guard: {
default: null
}
}
use: {
$app: Illuminate\Foundation\Application {#2 …}
}
file: "C:\project\newglit\myglit\vendor\laravel\framework\src\Illuminate\Auth\AuthServiceProvider.php"
line: "83 to 85"
}
#routeResolver: Closure() {#284
class: "Illuminate\Routing\Router"
this: Illuminate\Routing\Router {#26 …}
use: {
$route: Illuminate\Routing\Route {#221 …}
}
file: "C:\project\newglit\myglit\vendor\laravel\framework\src\Illuminate\Routing\Router.php"
line: "650 to 652"
}
+attributes: Symfony\Component\HttpFoundation\ParameterBag {#45
#parameters: []
}
+request: Symfony\Component\HttpFoundation\ParameterBag {#44
#parameters: []
}
+query: Symfony\Component\HttpFoundation\ParameterBag {#51
#parameters: []
}
+server: Symfony\Component\HttpFoundation\ServerBag {#47
#parameters: array:29 [
"DOCUMENT_ROOT" => "C:\project\newglit\myglit\public"
"REMOTE_ADDR" => "127.0.0.1"
"REMOTE_PORT" => "56590"
"SERVER_SOFTWARE" => "PHP 7.1.29 Development Server"
"SERVER_PROTOCOL" => "HTTP/1.1"
"SERVER_NAME" => "127.0.0.1"
"SERVER_PORT" => "8000"
"REQUEST_URI" => "/api/deletealiasname"
"REQUEST_METHOD" => "POST"
"SCRIPT_NAME" => "/index.php"
"SCRIPT_FILENAME" => "C:\project\newglit\myglit\public\index.php"
"PATH_INFO" => "/api/deletealiasname"
"PHP_SELF" => "/index.php/api/deletealiasname"
"HTTP_HOST" => "127.0.0.1:8000"
"HTTP_CONNECTION" => "keep-alive"
"CONTENT_LENGTH" => "15"
"HTTP_CONTENT_LENGTH" => "15"
"HTTP_ACCEPT" => "application/json, text/javascript, */*; q=0.01"
"HTTP_ORIGIN" => "http://127.0.0.1:8000"
"HTTP_X_REQUESTED_WITH" => "XMLHttpRequest"
"HTTP_USER_AGENT" => "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safar
i/537.36"
"CONTENT_TYPE" => "text/plain;charset=UTF-8"
"HTTP_CONTENT_TYPE" => "text/plain;charset=UTF-8"
"HTTP_REFERER" => "http://127.0.0.1:8000/user/profile/"
"HTTP_ACCEPT_ENCODING" => "gzip, deflate, br"
"HTTP_ACCEPT_LANGUAGE" => "en-US,en;q=0.9,nb;q=0.8"
"HTTP_COOKIE" => "XSRF-TOKEN=eyJpdiI6IkdPR0NtcGNXdEJxKzVwZERheEZSMlE9PSIsInZhbHVlIjoiXC9EbHBHSXM2U0c1TzdjUHhOT25BZ1RQeXdzd3l0WGFw
bmNhWWduakJFRU9ubVBcL3pJdzJMcTI1SCtqQnR4Sk05IiwibWFjIjoiYjk4Mzk3M2ZiOGJlOGZkOGNmYjk1NjBjZGQzNTY3YzIzOWUwNzliNDdjNjNhMzRhODZkOThmZGI2NzU
3MDc2OCJ9; laravel_session=eyJpdiI6IktNaHlnY29BZnI3VUlEcHpnUU1vU2c9PSIsInZhbHVlIjoiUkxWTkVWdHlLcEZGY0FDOXAxMWdaNHROa2szSUY3ZlhWRm94V2ox
ZWZSYlc4SDdcL3FSdUlVNGZ0YkFreHl4VHQiLCJtYWMiOiJlOGQ0MjYwZTRiN2ZkOTQwYWRmNTcxNjYyNTY0YmYyZjY5MjBkN2E3ZWMxZGM3YWViYzQ4ODcxYzUyZTg0MTE3In0
%3D"
"REQUEST_TIME_FLOAT" => 1559843573.3443
"REQUEST_TIME" => 1559843573
]
}
+files: Symfony\Component\HttpFoundation\FileBag {#48
#parameters: []
}
+cookies: Symfony\Component\HttpFoundation\ParameterBag {#46
#parameters: array:2 [
"XSRF-TOKEN" => "eyJpdiI6IkdPR0NtcGNXdEJxKzVwZERheEZSMlE9PSIsInZhbHVlIjoiXC9EbHBHSXM2U0c1TzdjUHhOT25BZ1RQeXdzd3l0WGFwbmNhWWduakJF
RU9ubVBcL3pJdzJMcTI1SCtqQnR4Sk05IiwibWFjIjoiYjk4Mzk3M2ZiOGJlOGZkOGNmYjk1NjBjZGQzNTY3YzIzOWUwNzliNDdjNjNhMzRhODZkOThmZGI2NzU3MDc2OCJ9"
"laravel_session" => "eyJpdiI6IktNaHlnY29BZnI3VUlEcHpnUU1vU2c9PSIsInZhbHVlIjoiUkxWTkVWdHlLcEZGY0FDOXAxMWdaNHROa2szSUY3ZlhWRm94V2o
xZWZSYlc4SDdcL3FSdUlVNGZ0YkFreHl4VHQiLCJtYWMiOiJlOGQ0MjYwZTRiN2ZkOTQwYWRmNTcxNjYyNTY0YmYyZjY5MjBkN2E3ZWMxZGM3YWViYzQ4ODcxYzUyZTg0MTE3In
0="
]
}
+headers: Symfony\Component\HttpFoundation\HeaderBag {#49
#headers: array:12 [
"host" => array:1 [
0 => "127.0.0.1:8000"
]
"connection" => array:1 [
0 => "keep-alive"
]
"content-length" => array:1 [
0 => "15"
]
"accept" => array:1 [
0 => "application/json, text/javascript, */*; q=0.01"
]
"origin" => array:1 [
0 => "http://127.0.0.1:8000"
]
"x-requested-with" => array:1 [
0 => "XMLHttpRequest"
]
"user-agent" => array:1 [
0 => "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36"
]
"content-type" => array:1 [
0 => "text/plain;charset=UTF-8"
]
"referer" => array:1 [
0 => "http://127.0.0.1:8000/user/profile/"
]
"accept-encoding" => array:1 [
0 => "gzip, deflate, br"
]
"accept-language" => array:1 [
0 => "en-US,en;q=0.9,nb;q=0.8"
]
"cookie" => array:1 [
0 => "XSRF-TOKEN=eyJpdiI6IkdPR0NtcGNXdEJxKzVwZERheEZSMlE9PSIsInZhbHVlIjoiXC9EbHBHSXM2U0c1TzdjUHhOT25BZ1RQeXdzd3l0WGFwbmNhWWduak
JFRU9ubVBcL3pJdzJMcTI1SCtqQnR4Sk05IiwibWFjIjoiYjk4Mzk3M2ZiOGJlOGZkOGNmYjk1NjBjZGQzNTY3YzIzOWUwNzliNDdjNjNhMzRhODZkOThmZGI2NzU3MDc2OCJ9;
laravel_session=eyJpdiI6IktNaHlnY29BZnI3VUlEcHpnUU1vU2c9PSIsInZhbHVlIjoiUkxWTkVWdHlLcEZGY0FDOXAxMWdaNHROa2szSUY3ZlhWRm94V2oxZWZSYlc4SD
dcL3FSdUlVNGZ0YkFreHl4VHQiLCJtYWMiOiJlOGQ0MjYwZTRiN2ZkOTQwYWRmNTcxNjYyNTY0YmYyZjY5MjBkN2E3ZWMxZGM3YWViYzQ4ODcxYzUyZTg0MTE3In0%3D"
]
]
#cacheControl: []
}
#content: null
#languages: null
#charsets: null
#encodings: null
#acceptableContentTypes: null
#pathInfo: "/api/deletealiasname"
#requestUri: "/api/deletealiasname"
#baseUrl: ""
#basePath: null
#method: "POST"
#format: null
#session: null
#locale: null
#defaultLocale: "en"
-isHostValid: true
-isForwardedValid: true
basePath: ""
format: "html"
}
答案 0 :(得分:1)
我想问题出在method:"post"
因为它不是method
对象,所以实际上是type
。
type:"post"
尝试这个
$.ajax({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
url: "{{ route('deletealiasname') }}",
type: "post",
data: {'alias_id': alias_id},
dataType: "json",
contentType: false,
cache: false,
processData: false,
beforeSend : function()
{
console.log(alias_id);
},
success: function (response) {
console.log(response);
}
});
与您所说的一样,您正在使用laravel 5.8
。因此您可以在终端中启动转储服务器以进行调试。终端php artisan dump-server
和dd(#request)
中的jsut hit命令
看看是否有数据。
确定问题出在method:"post"
上,只需用type:"post"
进行更改
答案 1 :(得分:0)
我不知道为什么会发生这种情况,但是找到了一种通过ajax发送数据的方法。我只是添加了一个form元素,然后将数据附加到FormData并将其作为ajax数据发送。
表单元素
<form id="deleteAliasName" class="ui form" action="{{ route('deletealiasname') }}" method="post">
</form>
Ajax
var alias_id = $(this).attr('data-id');
formData = new FormData($('#deleteAliasName')[0]);
formData.append('alias_id', alias_id);
$.ajax({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
url: "{{ route('deletealiasname') }}",
type: "post",
data: formData,
dataType: "json",
contentType: false,
cache: false,
processData: false,
success: function (response) {
console.log(response);
}
});
现在我在控制器请求中接收到数据
我不会将其标记为可接受的答案,因为我认为这是一种解决方法。