[英]how to send POST data with jquery ajax to PHP function
I want to send POST request with jquery AJAX to PHP function but it don't give any respond. 我想用jquery AJAX将POST请求发送到PHP函数,但它没有任何响应。
JQuery jQuery查询
$.ajax({
url: 'ListProduk.php/SelectData',
type: 'POST',
success: function(data)
{
console.log(data);
}
});
PHP PHP
<?php
include 'database.php';
function SelectData(){
$sql = "SELECT * FROM MsProduk";
$result = $conn->query($sql);
while($row = $result->fetch_assoc()){
$arr[] = $row;
}
return json_encode($arr);
}
?>
Any ideas how to fix it? 任何想法如何解决?
When not using url rewriting (google mod_rewrite and pretty URL's), your parameters typically are going to be passed as a normal HTTP GET
request. 当不使用url重写(google mod_rewrite和漂亮的URL)时,通常会将您的参数作为普通的HTTP
GET
请求传递。 Here is an example of how your URL structure might look: 这是您的URL结构外观的示例:
url: 'ListProduk.php?action=SelectData'
And then, in your PHP, you might handle it based on the action that is requested (Note: The action
parameter is not something specific to web development, it's just an arbitrary name I assigned. It could be foo=SelectData
as well) 然后,在您的PHP中,您可以根据请求的操作来处理它(注意:
action
参数不是特定于Web开发的东西,它只是我分配的任意名称。它也可以是foo=SelectData
)
if ($_POST['action'] == 'SelectData') {
// Run the code for selecting data
}
Finally, you wouldn't want to "return" the JSON data. 最后,您不想“返回” JSON数据。 You need to output it with the correct headers.
您需要使用正确的标题输出它。 It would look something like this:
它看起来像这样:
header('Content-Type: application/json');
echo json_encode($data);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.