【发布时间】:2015-09-17 12:40:16
【问题描述】:
我正在使用 Codeigniter。
我需要访问两次 php 函数,但第一次我不必回显 json_encode 数组....当我从 js 文件调用它时,我应该第二次回显它。我该怎么做?
所以,php 文件:
function filter(){
if (function call is not from js file ) {
//using $_POST variable to get info from the form
dont echo json
}elseif (function call is from the js file) {
echo json_encode($data);
//but $data should be $_POST info too, but in this case $_POST WOULD
//BE EMPTY and I'm sending no data to ajax
}
}
来自 js 文件:
$.ajax({
type: "GET",
url: "filter",
dataType: 'json',
success: function(data){
alert(data); },
error: function(data){
alert('error'); }
【问题讨论】:
-
为什么要调用同一个函数两次?
-
快速破解方法是在您的 ajax 中添加一个参数
data:并在其中存储一个变量。然后,您可以检查服务器端的变量。更多:stackoverflow.com/questions/4406348/… -
@light bc 我正在处理一个表单(过滤信息),如果需要某些信息,那么我必须继续过滤
-
@DipenShah 我认为我有太多信息无法通过帖子再次发送它:s 我知道这可能是一个解决方案,但是如果没有这个逻辑,还有其他方法可以做我想做的事吗?
-
@Limon 然后我认为纤毛病的解决方案一针见血。您需要检查请求是否为 XMLHTTPRequest。虽然这有效,但您可能想阅读此stackoverflow.com/questions/18260537/…
标签: javascript php jquery json