2016-10-03 98 views
0

我从这个AJAX调用中获得undefined index(return data)。我在这里做错了什么?请纠正我。AJAX请求在PHP中提供'undefined index'错误

$(document).ready(function() { 
    $('#something').click(function() { 
     edit(1, 'abc', 'xyz'); 
    }); 

    function edit(id, column, text) { 
     $.ajax({ 
      url: "edit.php", 
      method: "POST", 
      data: { 
       id: id, 
       text: text, 
       column: column 
      }, 
      dataType: "text", 
      success: function(data) { 
       console.log(data); 
      } 
     });  
    } 
}); 

edit.php

echo $_POST["id"]; 

我得到这个在控制台:在Ç。ID:

注意:未定义指数\ XAMPP \ htdocs中\数据表\ edit.php on line

+0

这错误意味着问题出在你的PHP代码,而不是JS。检查第2行的'edit.php'文件。第2行的 –

+0

此代码存在echo $ _POST [“id”] – EaBangalore

+0

是“edit.php”的路径是否正确?是:你有多个'edit.php'文件,你试图通过任何机会发布信息? – RamRaider

回答

0

我认为这个问题是因为你写json数组键的方式。

尝试使用双引号从数据JSON的钥匙,就像这样:

data:{"id":id, "text":text, "column":column} 

或变量和密钥使用不同的名称。

+0

没有答案是正确的 – EaBangalore

+0

嗨!我应该删除这个答案吗? – mihutz

+0

不要删除,如果你觉得它是正确的 – EaBangalore

0

我认为url:“edit.php”在您的服务器中没有访问权限。

通常情况下,如果你在你的服务器本地;所以你改变你的网址为:

“”。

$.ajax({ 
      url:"http://localhost/yourproject/edit.php", 
      method:"POST", 
      data:{id:id, text:text, column:column}, 
      dataType:"text", 
      success:function(data){ 
       console.log(data); 
      } 
     });  
0

试试这个,如果你想用PHP $ _POST来获取它,你可以添加内容类型的json。我不知道这个,但它的作品在我的角度

function edit(id, column, text) { 
    $.ajax({ 
     url: "edit.php", 
     method: "POST", 
     data: { 
      id: id, 
      text: text, 
      column: column 
     }, 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     success: function(data) { 
      console.log(data); 
     } 
    });  
} 
0

请使用此代码尝试

/* start */ 
$(document).ready(function() { 

$('#something').click(function() { 

    var sendData = { 
     url : '1', 
     method : 'abc', 
     column : 'xyz' 
    }; 

    edit(sendData); 

}); 

function edit(id, column, text) { 
    $.ajax({ 
     type: "POST", 
     url: "edit.php", 
     data: sendData, 
     success: function(response){ 
      console.log(data); 
     } 
    }); 
} 
}); 

/* end */ 
0

很多斗争的我得到了我的答案后。

even though after specifying method as POST ajax is sending it as a GET 

(为什么?我不知道是否有人知道请张贴您的答案)。

只有改变ID的确在(edit.php)

编辑。php

echo $ _GET ['id'];

2

尝试使用

类型: “POST”

,而不是

方法: “POST”

...$.ajax({ 
     url: "edit.php", 
     type: "POST", 
     data: { 
      id: id, 
      text: text, 
      column: column 
     }..... 

它仍然类这是一个GET请求?我认为这是因为你的jQuery版本。

docs

type (default: 'GET') 
Type: String 
An alias for method. You should use type if you're using versions of jQuery prior to 1.9.0. 
是否使用的是JQuery的版本
1

?尝试改变

method: "POST", 

type: "POST",