简体   繁体   English

Ajax PHP中的结果空白

[英]Blank result in Ajax PHP

Every time I do an Ajax request, I want server to check if number is 1, 3 or 5, and depending on result, echo out answer. 每次我执行Ajax请求时,我都希望服务器检查数字是1、3还是5,并根据结果回显答案。 The problem is, I get no data in my alert(); 问题是,我的alert();没有data alert(); . Here's my code: 这是我的代码:

ajax: 阿贾克斯:

ajaxNumber= $.ajax({
            url: "operations.php",
            type: "post",
            data: ile,
            success: function(data){alert(data);}
        });

PHP: PHP:

if(isset($_POST['ile'])) {
  $numer = intval($_POST['ile']);
  if($numer==1|$numer==3|$numer==5) {
  echo "ok";
 } else {
  echo "ERROR";
 }
}

alert(); is being displayed, but it's just empty. 正在显示,但它为空。

在此处输入图片说明

EDIT: Running operations.php on my own and giving $_POST['ile'] an existing value echos out me good responses. 编辑:我自己运行operations.php并给$ _POST ['ile']一个现有值,使我得到了很好的响应。

you don't set ile name so $_POST['ile'] is empty, try this : 您没有设置ile名称,因此$ _POST ['ile']为空,请尝试以下操作:

ajaxNumber= $.ajax({
        url: "operations.php",
        type: "post",
        data: {'ile':ile},
        success: function(data){alert(data);}
    });

Basically you can't pass ile with value 1, 3 or 5 literally. 基本上,您不能从字面上传递值1、3或5的ile

Change your code like below: 如下更改代码:

var ile = 1;// just a example so I assign 1 to variable `ile`
var postData = {'ile': ile};
$.ajax({
    url: "operations.php",
    type: "post",
    data: postData,
    success: function(response){
       alert(response);
    }
});

JQuery documentation says data is jQuery文档说数据是

Data to be sent to the server. 数据要发送到服务器。 It is converted to a query string. 它将转换为查询字符串。

Type: PlainObject or String or Array 类型:PlainObject或字符串或数组

So that's why you can't send 1, 3 or 5 directly, its supposed to be a object like {name1: value, name2: value2} or a query string like 'name1=value1&name2=value2' 因此,您不能直接发送1、3或5,它应该是{name1: value, name2: value2}类的对象{name1: value, name2: value2}或者是'name1=value1&name2=value2'类的查询字符串

EDIT 编辑

See your php code, in if condition please use logic OR || 查看您的php代码, if有条件请使用逻辑OR || like the other guy said in the comment. 就像其他人在评论中说的那样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM