简体   繁体   English

AJAX响应为空

[英]AJAX response empty

I've tried this like 8 hours and I don't get it. 我已经尝试了8个小时,但还是不明白。

With $.ajax I get datas from my database via PHP-script. 使用$.ajax我可以通过PHP脚本从数据库中获取数据。 But in this case it doesn't seem to work and I don't no why. 但是在这种情况下,它似乎不起作用,我也不是为什么。 The data2 is empty, no matter what. 无论如何, data2为空。

$.ajax({
  url: 'http://myurl.de/get', 
  data: [{ 'person_id': 2, 'action': 'getLinks' }],
  method: 'POST',
  success: function(data2){
    console.log(data2);
  }
});

The PHP-script (important parts) looks like this PHP脚本(重要部分)如下所示

function getLinks($person_id)
{
    /* sql here */  

    /* format sql-output here */

    return $output;
}

if($_POST['action'] == 'getLinks'){
    echo getLinks($_POST['person_id']);
}

The funny thing is, I have the exact AJAX-request in the JavaScript-file some lines above with another action and it works perfectly. 有趣的是,我在JavaScript文件中有确切的AJAX请求,在上面的几行中有另一个动作,它可以完美地工作。 When I try to get the data directly in the PHP-file I get the result. 当我尝试直接在PHP文件中获取数据时,我得到了结果。 The return $output is always with the data, but in don't comes to the JavaScript file. 返回的$output总是与数据一起出现,但是不在JavaScript文件中。

The AJAX always calls the success-function, but with no data2. AJAX始终调用成功函数,但是没有data2。

Try using only object as data , without wrapping it in an array: 尝试仅将对象用作data ,而不将其包装在数组中:

$.ajax({
  url: 'http://myurl.de/get', 
  data: { 'person_id': 2, 'action': 'getLinks' },
  method: 'POST',
  success: function(data2){
    console.log(data2);
  }
});

Try like this 这样尝试

   $.ajax({ url: 'http://myurl.de/get', 
                data: { 'person_id': 2, 'action': 'getLinks' },
                method: 'POST',
                success: function(data2){
                    console.log(data2);
                }
        });

I've updated data line 我更新了数据行

use this: 用这个:

data: { 'person_id': 2, 'action': 'getLinks' },

Instead of 代替

data: [{ 'person_id': 2, 'action': 'getLinks' }],

data can be an object or string or an array . 数据可以是objectstringarray When you are sending data as 当您发送data

data: [{ 'person_id': 2, 'action': 'getLinks' }],

it will be basically [>Object] which is an array of object. 基本上是[>Object] ,它是一个对象数组。 You can still send in this way if there is a proper mechanism to retrieve it in the server side. 如果在服务器端有适当的机制来检索它,您仍然可以这种方式发送。 But sending { 'person_id': 2, 'action': 'getLinks' } you are sending data as object & I presume you server side is configured to accept data in this format 但是发送{ 'person_id': 2, 'action': 'getLinks' }您将数据作为object发送,并且我假设服务器端已配置为接受这种格式的数据

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

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