我展示了一个标签列表,我有一个小表格,可以将另一个标签添加到该列表中。
我使用ajax而不是使用交响乐学说完成此表单,因为我想动态地向列表添加内容而不是页面请求。
下面的HTML代码显示了我创建表单的方式:
<div class="tag-form">
<form id="tag-form" name="tagging-form" action="{{path('media_bulk_tag_route') }}" method="post">
<input placeholder="Enter Tag" class="add-tag-input">
<button type="submit">Add</button>
</form>
</div>
然后我使用Jquery ajax提交此表单:
$('#tag-form').on('submit',function(e){
e.preventDefault();
var data = $('form[name="tagging-form"]').serialize();
$.ajax({
type: 'POST',
url: $(this).attr('action'),
data: {request: data},
success: function (resp) {
var d = $.parseJSON(JSON.stringify(resp));
console.log(d);
},
complete: function () {
}
})
});
在我的控制器中,我收到了这个请求,只是var转储它。
/**
* Bulk tagging
*
* @Route("/media-tagging", name="media_bulk_tag_route")
*/
public function mediaBulkTag(Request $request)
{
$request->request->get('request');
VarDumper::dump($request);
die();
}
现在问题是当我在网络控制台中读取此var转储时,数据为空。为什么会这样,我还需要做些什么。感谢。
这是完整转储:
DatabaseController.php on line 125:
Request {#9
+attributes: ParameterBag {#12
#parameters: array:3 [
"_controller" => "MyBundle\Controller\DatabaseController::mediaBulkTag"
"_route" => "media_bulk_tag_route"
"_route_params" => []
]
}
+request: ParameterBag {#10
#parameters: array:1 [
"request" => ""
]
}
+query: ParameterBag {#11
#parameters: []
}
+server: ServerBag {#15
#parameters: array:36 [
"USER" => "vagrant"
"HOME" => "/home/vagrant"
"HTTP_COOKIE" => "Phpstorm-5751b086=1fbc46ec-4192-42ec-8d23-628b66cfe7b4; adminer_version=0; PHPSESSID=hmethpqn6bsq493vpuejbg4ulm"
"HTTP_ACCEPT_LANGUAGE" => "en-GB,en-US;q=0.8,en;q=0.6"
"HTTP_ACCEPT_ENCODING" => "gzip, deflate, br"
"HTTP_REFERER" => "http://localhost:8000/media-database"
"HTTP_X_REQUESTED_WITH" => "XMLHttpRequest"
"HTTP_ACCEPT" => "*/*"
"HTTP_CONTENT_TYPE" => "application/x-www-form-urlencoded; charset=UTF-8"
"HTTP_USER_AGENT" => "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"
"HTTP_ORIGIN" => "http://localhost:8000"
"HTTP_CONTENT_LENGTH" => "8"
"HTTP_CONNECTION" => "keep-alive"
"HTTP_HOST" => "localhost:8000"
"REDIRECT_STATUS" => "200"
"SERVER_NAME" => "my.bundle"
"SERVER_PORT" => "80"
"SERVER_ADDR" => "10.0.2.15"
"REMOTE_PORT" => "55895"
"REMOTE_ADDR" => "10.0.2.2"
"SERVER_SOFTWARE" => "nginx/1.11.9"
"GATEWAY_INTERFACE" => "CGI/1.1"
"SERVER_PROTOCOL" => "HTTP/1.1"
"DOCUMENT_ROOT" => "/home/vagrant/Code/my-bundle/web"
"DOCUMENT_URI" => "/app_dev.php"
"REQUEST_URI" => "/media-tagging"
"SCRIPT_NAME" => "/app_dev.php"
"SCRIPT_FILENAME" => "/home/vagrant/Code/my-bundle/web/app_dev.php"
"CONTENT_LENGTH" => "8"
"CONTENT_TYPE" => "application/x-www-form-urlencoded; charset=UTF-8"
"REQUEST_METHOD" => "POST"
"QUERY_STRING" => ""
"FCGI_ROLE" => "RESPONDER"
"PHP_SELF" => "/app_dev.php"
"REQUEST_TIME_FLOAT" => 1496303912.2553
"REQUEST_TIME" => 1496303912
]
}
+files: FileBag {#14
#parameters: []
}
+cookies: ParameterBag {#13
#parameters: array:3 [
"Phpstorm-5751b086" => "1fbc46ec-4192-42ec-8d23-628b66cfe7b4"
"adminer_version" => "0"
"PHPSESSID" => "hmethpqn6bsq493vpuejbg4ulm"
]
}
+headers: HeaderBag {#16
#headers: array:13 [
"cookie" => array:1 [
0 => "Phpstorm-5751b086=1fbc46ec-4192-42ec-8d23-628b66cfe7b4; adminer_version=0; PHPSESSID=hmethpqn6bsq493vpuejbg4ulm"
]
"accept-language" => array:1 [
0 => "en-GB,en-US;q=0.8,en;q=0.6"
]
"accept-encoding" => array:1 [
0 => "gzip, deflate, br"
]
"referer" => array:1 [
0 => "http://localhost:8000/media-database"
]
"x-requested-with" => array:1 [
0 => "XMLHttpRequest"
]
"accept" => array:1 [
0 => "*/*"
]
"content-type" => array:1 [
0 => "application/x-www-form-urlencoded; charset=UTF-8"
]
"user-agent" => array:1 [
0 => "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"
]
"origin" => array:1 [
0 => "http://localhost:8000"
]
"content-length" => array:1 [
0 => "8"
]
"connection" => array:1 [
0 => "keep-alive"
]
"host" => array:1 [
0 => "localhost:8000"
]
"x-php-ob-level" => array:1 [
0 => 1
]
]
#cacheControl: []
}
#content: null
#languages: null
#charsets: null
#encodings: null
#acceptableContentTypes: null
#pathInfo: "/media-tagging"
#requestUri: "/media-tagging"
#baseUrl: ""
#basePath: null
#method: "POST"
#format: null
#session: Session {#176
#storage: NativeSessionStorage {#175
#bags: array:2 [
"attributes" => AttributeBag {#171
-name: "attributes"
-storageKey: "_sf2_attributes"
#attributes: & array:1 [
"_csrf/form" => "177Yjq_CGVSRLnuBjNTVcsQ3a5Tgzi5-lC8DQDpcE9g"
]
}
"flashes" => FlashBag {#170
-name: "flashes"
-flashes: & []
-storageKey: "_sf2_flashes"
}
]
#started: true
#closed: false
#saveHandler: SessionHandlerProxy {#172
#handler: NativeFileSessionHandler {#174}
#wrapper: true
#saveHandlerName: "files"
}
#metadataBag: MetadataBag {#173
-name: "__metadata"
-storageKey: "_sf2_meta"
#meta: & array:3 [
"u" => 1496303912
"c" => 1496303203
"l" => "0"
]
-lastUsed: 1496303903
-updateThreshold: "0"
}
}
-flashName: "flashes"
-attributeName: "attributes"
}
#locale: null
#defaultLocale: "en"
-isForwardedValid: true
}
答案 0 :(得分:1)
输入没有name
标记
改变这个:
<input placeholder="Enter Tag" class="add-tag-input">
以此为例
<input placeholder="Enter Tag" class="add-tag-input" name="your-input">