weixin_33736832 2016-02-20 13:13 采纳率: 0%
浏览 84

AJAX响应为空

I've tried this like 8 hours and I don't get it.

With $.ajax I get datas from my database via PHP-script. But in this case it doesn't seem to work and I don't no why. The data2 is empty, no matter what.

$.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

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. When I try to get the data directly in the PHP-file I get the result. The return $output is always with the data, but in don't comes to the JavaScript file.

The AJAX always calls the success-function, but with no data2.

  • 写回答

3条回答 默认 最新

  • ℙℕℤℝ 2016-02-20 13:17
    关注

    Try using only object as data, without wrapping it in an array:

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

报告相同问题?