Error al intentar analizar un objeto json en jquery

I used jquery-ajax to search a particular variable in database and return it back ,so that I can append it on my html page. The php file which searches in the database is

$q="SELECT abc FROM xyz where axb='".$_GET['var']."'";
$sql = mysql_query($q) or die(mysql_error());

$row = mysql_fetch_array($sql);

$temp=$row['company'];

$k['na'] = $temp;

echo json_encode($k);
 ?> 

This file is showing the correct output if I try using the URL which is

{"na":"Microsoft Corporation"}

but the jquery which should parse it and append to a div is returning null Here is the code

function name() {
    $.ajax({
        type: 'GET',
        async: false,
        dataType: "json",
        url: dir + 'name.php?tick=' + $("#tickerid").val(),
        success: function(name) {
            alert("hello");
            alert(name['na']);
            $("#nam").html('<p>' + name['na'] + '</p>');
        }
    });
}

but if I place a string in the file in place of $row[''];

$temp=$row['company'];
//$k['na'] = $temp;

$k['na'] = "Microsoft";
echo json_encode($k);

The url output is same {"na":"Microsoft"} and the jquery is successful appending it in the html.

Can anyone tell me what has gone wrong, since the php is returning the same thing for both the variable and string , what is wrong with my jquery code which is trying to retrieve it??

preguntado el 28 de agosto de 12 a las 09:08

Do you get any errors in your browser's console? -

@AnthonyGrist Thanks to making me correct, didn't knew this.. -

What URL are you putting into your browser when testing it without AJAX? What's the value of dir - if you're using a relative URL, are you sure it's correct? Also, in your jQuery code you're specifying a tick parameter, but in your PHP you seem to be (I don't really know PHP) using a var GET parámetro. -

I hope this is test code only, else.. put mysql_real_escape_string around your get variable to prevent SQL injection! -

thanks for the reply guys Anthony: i dint get any error using browser console , i even printed both of the o/p which it returned , first one when i used to run it correctly and second hard coding it no the sql query was just an example and im sure its returning correctly , tick and var are just examples . the main problem how could the jquery append the result when it is hard coded and why not when the actual code is returnig when in both the cases the result same for php using browser console -

0 Respuestas

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.