【发布时间】:2015-09-18 23:45:02
【问题描述】:
我发送了一个 ajax 发布请求,并得到了响应,但它是空的。是否有一些细节需要调整?
这个想法是发送通过提交按钮提交的文本。但只是为了测试,我已经指定要发送的数据为“url”:“hello”。
$(document).ready(function() {
$('form.ajax').on('submit', function(e) {
e.preventDefault();
var submitted = $(this),
destination = submitted.attr('action'),
method = submitted.attr('method'),
url_send = {
"url": "helllo"
};
$.ajax({
type: method,
contentType : 'application/json',
url: destination,
data: url_send,
success: function(response){
console.log(response);
},
error: function(){
console.log("there was an error");
}
});
表格中指定了“方法”(post)和“目的地”。所以“url_send”是发送的对象。这应该在另一端检索为 {"url": "hello"},还是嵌套在对象中?
在带有 laravel 的 PHP 中,我有一个接收请求的控制器函数:
$data = json_decode(file_get_contents("php://input"));
如果 $data 为空,则返回:
return Response::json($data);
这给了我
Object { }
【问题讨论】:
标签: php jquery ajax post laravel-4