Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jqueryajax-错误响应_Javascript_Php_Jquery_Json_Ajax - Fatal编程技术网

Javascript jqueryajax-错误响应

Javascript jqueryajax-错误响应,javascript,php,jquery,json,ajax,Javascript,Php,Jquery,Json,Ajax,我对jQuery ajax函数有一个问题: var arrayIdEq = JSON.stringify(iditem); $.ajax({ type: "POST", url: "index.php", dataType : 'text', contentType: 'application/json; charset=utf-8', data: { arrayIdEq :

我对jQuery ajax函数有一个问题:

var arrayIdEq = JSON.stringify(iditem);
    $.ajax({
        type: "POST",   
        url: "index.php",
        dataType : 'text', 
        contentType: 'application/json; charset=utf-8',
        data: {
            arrayIdEq : arrayIdEq
        },
        success: function(answer) {
            alert(answer);
        },
        complete: function() {
        },
        error: function(jqXHR, errorText, errorThrown) {
            alert(jqXHR+" - "+errorText+" - "+errorThrown);
        }
    });
“arrayIdEq”包含从0到7的数字,或字符串“EMPTY”。 PHP代码:

elseif(isset($_POST['arrayIdEq'])){ 
$answer = "my_answer";
return $answer;
请求后,当成功响应到来时,警报显示。。。但问题是。警报包含的不是“$answer”值,而是。。。HTML代码从我的主页

<!DOCTYPE html>
<html lang="pl">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title> Medivia</title>
</head>
<body>
<h1>Medivia</h1>
    <form action="index.php" method="POST">
    <label>E-mail:<br><input type="text" name="mail" required></label>
    <br>
    <label>Hasło:<br><input type="password" name="pass" required></label>
    <br>
    <button name="login">Zaloguj się</button>
</form>
</body>
</html>

麦迪维亚
麦迪维亚
电子邮件:

Hasło:

扎洛古吉·西ę

我不知道这里发生了什么。有人能解释一下那里发生了什么事吗?我做错了什么

函数
success
中的
answer
变量将包含php脚本的完整输出

因此,当您调用
index.php
并执行以下操作时:

elseif(isset($_POST['arrayIdEq'])){ 
  $answer = "my_answer";
  return $answer;
}
仅当从主脚本(不是从函数内部)调用
return
语句时,脚本才会退出,但输出将是脚本在此之前生成的输出

您的脚本应该只向javascript输出您想要返回的内容,而不是返回


与使用用于构建完整页面的
index.php
文件相比,ajax请求的单独脚本可能是一个更方便的解决方案。

您正在将ajax请求发布到:
index.php
。如果你的PHP代码不在那里,那么它只会返回该页面。你有没有在PHP中输出过
$answer
?@chris85我没有。@Magnus Eriksson But index.PHP这是我保存PHP代码的地方。@Petrus这是你的问题。AJAX接收您在发送请求的页面上输出的内容。我将“return”替换为“echo”。但结果和以前一样。@Petrus脚本中只有
?@chris85是的,它可以工作!:)抱歉搞砸了,英语不是我的母语。还有很多东西要学。谢谢你的帮助!