简体   繁体   English

AJAX调用成功,但引发未定义索引错误

[英]AJAX call success but throws Undefined index error

I have an ajax call that passes data to another php file, createTest2.php, as below. 我有一个ajax调用,它将数据传递到另一个PHP文件createTest2.php,如下所示。

But the createTest2.php file throws error 但是createTest2.php文件抛出错误

"Notice: Undefined index: aaa in C:\\xampp\\htdocs\\TestProj\\Test\\createTest2.php on line 2 “注意:未定义的索引:第2行的C:\\ xampp \\ htdocs \\ TestProj \\ Test \\ createTest2.php中的aaa

caller.php caller.php

$(document).ready(function(){
    $("#button_submit").click(function() 
  {

    $.ajax({
      type:"POST",
      url:"createTest2.php",
      data:{aaa : "UNIT_TEST"},
      success:function()
      {
        alert("success");
      }
    });
 });
});

createTest2.php createTest2.php

$test_name = $_POST['aaa'];

there is nothing wrong on your code/script. 您的代码/脚本没有错。 i tried it on my localhost and it works. 我在我的本地主机上尝试过,它可以工作。

make sure that the error reporting is configured correctly because it is printing a notice that will also be passed to your ajax result. 确保错误报告配置正确,因为它正在打印一条通知,该通知也将传递给您的ajax结果。

to remove it use these on top of php code of your createTest2.php : (before you call anything, just after the first php opening tag) 要删除它,请在createTest2.php的php代码之上使用这些代码(在调用任何内容之前,在第一个php开头标记之后)

<?php
ini_set("DISPLAY_ERRORS",0);
error_reporting(0);

take note that it is only a NOTICE and not an ERROR as indicated on your question. 请注意,它只是一个NOTICE而不是您的问题所指示的ERROR hope that helps. 希望能有所帮助。 ;) ;)

** UPDATE: some people say it is recommended to fix the notices instead of just hiding them, just so you know.. **更新:有人说,建议您修正通知,而不是仅仅隐藏它们,这就是您所知道的。

Give data attribute in quotes, 给数据属性加引号,

Change 更改

 data:{aaa : "UNIT_TEST"},

To

data:{"aaa" : "UNIT_TEST"},

try to send is as an data query ( using param to convert object to an data query) 尝试以数据查询的形式发送(使用param将对象转换为数据查询)

$(document).ready(function(){
    $("#button_submit").click(function() 
  {
    var params = $.param({aaa : "UNIT_TEST"});

    $.ajax({
      type:"POST",
      url:"createTest2.php",
      data: params,
      success:function()
      {
        alert("success");
      }
    });
 });
});

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

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