Javascript 将数据从JQuery的点击函数传递到PHP服务器?
我通过一个简单的函数调用REST服务器:Javascript 将数据从JQuery的点击函数传递到PHP服务器?,javascript,php,jquery,Javascript,Php,Jquery,我通过一个简单的函数调用REST服务器: $(document).on('click', '#test', function(e){ e.preventDefault(); var name = $(this).data('name'); var path = $(this).data('path'); window.location = "http://localhost:80/server/api/v1/files/" + name;
$(document).on('click', '#test', function(e){
e.preventDefault();
var name = $(this).data('name');
var path = $(this).data('path');
window.location = "http://localhost:80/server/api/v1/files/" + name;
});
如何将传统数据(如路径)传递到服务器,以便能够在php请求中获取它
public function show($request, $response, $args)
{
// $request contains the path
}
在Jquery中,可以使用ajax
$(document).on('click', '#test', function(e){
e.preventDefault();
var name = $(this).data('name');
var path = $(this).data('path');
$.ajax({
'url' : 'http://localhost:80/server/api/v1/files/',
'method' : 'POST',
'data' : {
'name' : name,
'path' : path
},
success(function(data){
console.log("Sent data");
})
});
});
只是出于好奇:没有ajax是否可能实现同样的效果?是的,您可以将参数作为argsOkay传递,这是可能的,但没有意义,因为在我看来,它违反了REST最佳实践。是的,它确实违反了REST实践