1

I'm trying to get some data trough AJAX but am getting an error with an empty responseText.

My code is as follows:

JS:

function getFounder(id) {
    var founder = "";

    $.ajax({ 

        url: '/data/founder_info.php',
        data: {founder: id},
        dataType: 'json',
        async: false,
        type: 'post',
        success: function(json) {

            //founder = json.username;

            console.log(json);

        },
        error: function(ts) {
            console.log("Error: " + ts.responseText);
        }
    });

    return founder;
}

PHP:

<?php

require_once '../core/init.php';

if($_POST['founder']) {

    $u = new User();

    $user_info = $u->find(escape($_POST['founder']));
    $user_info = $u->data();

    echo json_encode($user_info);
    exit();
}

I cannot find the issue as to why it is throwing the error.

Chris
  • 1,574
  • 4
  • 16
  • 49
  • 1
    What is the output of : console.log(json); – Mayank Pandeyz Aug 29 '16 at 09:56
  • @MayankPandey It wont even get to that as it throws the error – Chris Aug 29 '16 at 09:56
  • 1
    In that case check console to get the error in php call – Mayank Pandeyz Aug 29 '16 at 09:57
  • 3
    Look in your network tab in the Developers tools of your browser. See what the response is – Patrick Evans Aug 29 '16 at 09:57
  • 1
    `am getting an error` - that's a good start. A good next step would be to share with us the error you are getting, because there are so many possibilities, we could be here all night before we stumbled upon what the error is - you have an advantage, you obviously have an error message! – Jaromanda X Aug 29 '16 at 09:59
  • as an aside - synchronous XMLHttpRequests (yes, that's what you have there) are deprecated in most decent browsers these days - when you get this issue resolved, learn how to deal with asynchronous requests (and code in general) – Jaromanda X Aug 29 '16 at 10:01
  • @JaromandaX The error responseText is blank – Chris Aug 29 '16 at 10:02
  • @PatrickEvans I see no errors in the network tab – Chris Aug 29 '16 at 10:03
  • yes, because it would be, an error wont have responseText, as there is no responseText if there's an error (usually) ... console.log(ts); then inspect the object – Jaromanda X Aug 29 '16 at 10:03
  • You need to display errors in your php script or check the web-server error log. – jeroen Aug 29 '16 at 10:03
  • at first glance `$user_info = $u->find(escape($_POST['founder']));$user_info = $u->data();` - this looks wrong – Jaromanda X Aug 29 '16 at 10:04
  • @Chris, didn't say you would, said look at the response, meaning find the request that matches `/data/founder_info.php` and see what the server responded with – Patrick Evans Aug 29 '16 at 10:04
  • @PatrickEvans The reponse tab is empty as well. What does this mean? – Chris Aug 29 '16 at 10:06
  • have a look at the jquery documentation about [$.ajax](http://api.jquery.com/jquery.ajax/) - note that there are THREE parameters passed to the error callback - you've cherry picked a single property of the first one as a error message - you picked wrong, try all three arguments to get a real sense of what you've done wrong – Jaromanda X Aug 29 '16 at 10:07
  • the response tab will be empty ( `ts.responseText` proved that ) - which browser are you using - perhaps you need more precise guidance in how to use your browsers developer tools for debugging – Jaromanda X Aug 29 '16 at 10:08
  • @JaromandaX using Chrome – Chris Aug 29 '16 at 10:11
  • Means your php script isn't outputting anything, look at your server logs make sure the script isn't silently erroring out, check that `$u->data()` actually returns something that can encoded into JSON – Patrick Evans Aug 29 '16 at 10:11
  • @PatrickEvans I used `die()` to check on that. It outputs the correct data. – Chris Aug 29 '16 at 10:12
  • so, you don't even see `/data/founder_info.php` in the developer tools network tab? if so, can you see the status (is it 200? 404? 500? something else) – Jaromanda X Aug 29 '16 at 10:14
  • @JaromandaX It has status 200 – Chris Aug 29 '16 at 10:16
  • ok, so now change your `error:` to `error: function(a, b, c) { console.log(a, b, c); }` - see if there's anything at all useful logged in the console – Jaromanda X Aug 29 '16 at 10:18

1 Answers1

0

I fixed it using a method to convert everything to UTF-8.

function utf8ize($d) {
    if (is_array($d)) 
        foreach ($d as $k => $v) 
            $d[$k] = utf8ize($v);

     else if(is_object($d))
        foreach ($d as $k => $v) 
            $d->$k = utf8ize($v);

     else 
        return utf8_encode($d);

    return $d;
}

This was posted as an answer to this question

My problem is now solved!

Community
  • 1
  • 1
Chris
  • 1,574
  • 4
  • 16
  • 49