我正在尝试使用cURL重现Ajax请求,但它总是失败。 服务器根本没有查看发送的数据(下面的例子中的my_data)。
curl \
--request POST \
--header 'Content-type: application/json' \
--header 'X-Requested-With: XMLHttpRequest' \
--url 'http://example.org/ajax_call' \
--data '{"my_data":"data_value"}'
我也尝试过:
curl \
--request POST \
--header 'Content-type: application/json' \
--header 'X-Requested-With: XMLHttpRequest' \
--url 'http://example.org/ajax_call' \
--data "my_data":"data_value"
最后:
curl \
--request POST \
--header 'Content-type: application/json' \
--header 'X-Requested-With: XMLHttpRequest' \
--url 'http://example.org/ajax_call' \
-F "my_data=data_value"
数据永远不会被服务器捕获(在我的情况下是Symfony)。我想在cURL中存在XMLHttpRequest格式的问题,我想知道我正在尝试做什么甚至是可能的(意味着通过cURL正确格式化XMLHttpRequest对象)
我爬网几个小时,到目前为止还没有找到任何数据参数。
感谢您的帮助
修改
感谢评论部分的人员发现的解决方案:
curl -v \
--header 'X-Requested-With: XMLHttpRequest' \
'http://example.org/ajax_call' \
-d my_data=data_value
答案 0 :(得分:0)
您的所有cURL请求均有效且可以处理。在Symfony app controller中,您可以通过Request对象的方法getContent()
访问已发送的JSON对象:
/**
* @Route("/ajax_call", name="some_action")
*/
public function someAction(Request $request)
{
if ('' !== $content = $request->getContent()) {
$data = json_decode($content);
// some action…
}
return new Response(null, Response::HTTP_BAD_REQUEST);
}
在开发人员工具栏中,您的请求如下所示: