Ajax 如何从$post通话中获取数据?
我不想在搜索数据库(如在post上)时刷新页面,因此我在使用Ajax 如何从$post通话中获取数据?,ajax,Ajax,我不想在搜索数据库(如在post上)时刷新页面,因此我在使用$.post调用时获得了帮助,该调用可用于发送信息。有一个.done(function(data){行我还没有用过 我也遇到了这个问题,我不确定这是否与我的问题有关 我试图搜索数据库,字符串匹配,并返回带有匹配字符串的行。但我不想刷新页面,所以我认为我使用的是$.post调用和由javascript(一个按钮)触发的.done(函数(数据){) 所以我有两个部分,我所在的页面和一个单独的PHP页面,在调用时处理调用 如何建立一个可以返
$.post
调用时获得了帮助,该调用可用于发送信息。有一个.done(function(data){
行我还没有用过
我也遇到了这个问题,我不确定这是否与我的问题有关
我试图搜索数据库,字符串匹配,并返回带有匹配字符串的行。但我不想刷新页面,所以我认为我使用的是$.post调用和由javascript(一个按钮)触发的.done(函数(数据){
)
所以我有两个部分,我所在的页面和一个单独的PHP页面,在调用时处理调用
如何建立一个可以返回数据的桥?或者有更简单的方法吗?方法.done(function(){})
正是您想要使用的,但是您也可以查看$.post
函数的第三个参数(回调)
在服务器端,执行所有查询并准备jsoned数组中的内容,如:
// set up data to send
$contentArray = [
'content' => 'Some content',
'foo' => 'bar',
];
$jsonResults = json_encode($contentArray);
// you can always send header('Content-Type: application/json'); instead of using simple die function.
die($jsonResults);
然后在客户端:
<div class="content-container"></div>
<script type="text/javascript">
function someFunc() {
(...)
$.post(addr, values, function(res) {
var response = $.parseJSON(res);
$('.content-container').html(response.content);
});
}
</script>
someScript.php:
<?php
// you should force your script to allow only XML HTTP request here
if(empty($_SERVER['HTTP_X_REQUESTED_WITH']) || strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
die('AJAX requests only..');
}
// always remember to escape somehow your post values before you use them
$contentId = is_numeric($_POST['contentId']) ? intval($_POST['contentId']) : null;
if (null == $contentId) (...) // throw exception or return status=false
// do your mysql query like: "SELECT * FROM content WHERE id=".$contentId;
// well, it would be better to send headers instead of that
die(json_encode([
'status' => true, // this is a good practice to send some info, if everything is fine, if mysql row has been found etc..
'result' => $result, // mysql row, this is just in case you need other values to display
'content' => $result['content'], // I assume you have 'content' column in your mysql
]));
?>
true,//如果一切正常,如果发现mysql行等,发送一些信息是一个很好的做法。。
'result'=>$result,//mysql行,这只是为了防止需要显示其他值
'content'=>$result['content'],//我假设您的mysql中有'content'列
]));
?>
看一看Ajax的应用程序,那里有很多有用的信息
简而言之,您可以这样做:
function myPost() {
// Set the data
var data = {
'key' : 'value',
'key_2' : 'value_2'
};
// Do the post
$.post( '/your-url/', data, callBack );
}
function callBack( data ) {
// If the $.post was successful
success: function( data ) {
// do stuff
console.log( data ); // returned from your endpoint
},
// If there was an error
error: function( jqXHR, textStatus ) {
// do stuff
console.log( "Request failed: " + textStatus );
}
}
// On click of your element, fire the post request
$('#element').on('click', function() {
myPost();
});
我得到这个错误“未终止的正则表达式文字”我几乎用了你的代码逐字逐句,但我会再检查一遍。无论如何,谢谢我必须让它工作。不管这是我的错,最终找到了如何在控制台上导航错误的地方,这是一个注释代码问题。我也试过这个解决方案,我显然不知道我在做什么,什么都没有发生。conso中没有消息le说“成功或失败”,html没有变化,我必须仔细阅读。但谢谢你们的回答。
function myPost() {
// Set the data
var data = {
'key' : 'value',
'key_2' : 'value_2'
};
// Do the post
$.post( '/your-url/', data, callBack );
}
function callBack( data ) {
// If the $.post was successful
success: function( data ) {
// do stuff
console.log( data ); // returned from your endpoint
},
// If there was an error
error: function( jqXHR, textStatus ) {
// do stuff
console.log( "Request failed: " + textStatus );
}
}
// On click of your element, fire the post request
$('#element').on('click', function() {
myPost();
});