1

I am not to getting the values from the result of the AJAX call. I return JSON but I do not know how to get a value. This is my code:

$.ajax({ 
    method: 'POST',
    dataType: 'json',
    url: 'queryProduct.php',
    data: { codigo: cod }                   
}).done(function(response){

    // How do it get values???
}); 

PHP file result:

$query = "SELECT * FROM produccion.ma_producto WHERE codigo={$codigo}"; 
$result = pg_query($conn, $query);  

if (!$result) {
    echo "Error query: " . pg_last_error($conn);
} else {
    header('Content-type: application/json; charset=utf-8');
    echo json_encode($result);
}

From the first jquery call php file with ajax and after I would like to get the values and set to others elements....

ruzD
  • 555
  • 1
  • 15
  • 29
  • `response` will already be parsed to an object for you as you set `dataType: 'json'`, so calling `JSON.parse()` again will be causing an error. Remove that line, and just use `response` as you would any normal object. – Rory McCrossan Oct 13 '16 at 12:46
  • Have you watched the AJAX request / response in the browser's developer tools? Have you included the jQuery library in the project? Are there any errors reported? Are you running this on a web-server? – Jay Blanchard Oct 13 '16 at 12:46
  • @Rory McCrossan I have deleted the line, thanks! But I do not know how do get a value to use it? – ruzD Oct 13 '16 at 12:49
  • make sure you pass array properly – Nikhil Vaghela Oct 13 '16 at 12:51
  • possible duplicate of http://stackoverflow.com/questions/17364091/parse-json-response-using-jquery – Jay Blanchard Oct 13 '16 at 13:02
  • possible duplicate of http://stackoverflow.com/questions/3858698/how-to-read-json-response-as-name-value-pairs-in-jquery – Jay Blanchard Oct 13 '16 at 13:03

2 Answers2

1

$row = pg_fetch_row($result) echo json_encode($row);

Use above code in your PHP file.

Consider AJAX call returns this JSON Array.

i.e.

response = [
        {
            color: "red",
            value: "#f00"
        },
        {
            color: "green",
            value: "#0f0"
        },
        {
            color: "blue",
            value: "#00f"
        },
        {
            color: "cyan",
            value: "#0ff"
        },
        {
            color: "magenta",
            value: "#f0f"
        },
        {
            color: "yellow",
            value: "#ff0"
        },
        {
            color: "black",
            value: "#000"
        }
    ]

Now, after success of AJAX call you want to iterate through each JSON Object of this array, Where each Object is { color: 'someValue', value : 'someValue'}. You can use item.color and item.value to access them.

You can do something like this:

 $.ajax({
 method: 'POST',
 dataType: 'json',
 url: 'queryProduct.php',
 data: { codigo: cod },
 success: function(response) {                                   
     console.log(response.pagino);
});

Here, response is your JSON Array and item represents an Object of JSON Array.

You can use item.fieldName to access each of your field from each JSON Object.

gschambial
  • 1,383
  • 2
  • 11
  • 22
0

the values are within the callback function parameter "response", you can make an alert or a console.log and see them

$.ajax({ 
    method: 'POST',
    dataType: 'json',
    url: 'queryProduct.php',
    data: { codigo: cod }                   
}).done(function(response){
alert(response)
}); 
mondersky
  • 441
  • 2
  • 15