7

I'm trying to get data from data.php via jQuery ajax call.

My code looks like this:

var jsonData;

$.ajax({
        url: 'data.php',
        success: function(response) {
            jsonData = response;
        }
});

My data.php file is returning json formatted data but some text is in Unicode format. I set charset on data.php and on my javascript file, but still cant access responced data objects.

Any ideas?

  • 1
    Are you trying to access your `jsonData` variable immediately after calling `$.ajax()`? It would be `undefined` at that point because your ajax call is asynchronous. Do your processing inside the `success` handler. (If that's not it, please click "edit" and add detail about what "can't access" actually means in this case. What actually ends up in your `response` variable?) – nnnnnn Oct 20 '13 at 09:49

5 Answers5

14

Try to put dataType: 'json' in you ajax call:

var jsonData;

$.ajax({
        url: 'data.php',
        dataType: 'json',
        success: function(response) {
            jsonData = response;
        }
});
hjpotter92
  • 78,589
  • 36
  • 144
  • 183
zur4ik
  • 6,072
  • 9
  • 52
  • 78
2

Also you can use this mechanism:

$.getJSON( "data.php", function( response ) {
    jsonData = response;
});

It is more clean if you want get only JSON :)

Max
  • 1,824
  • 13
  • 22
1

You should be using header() function in your PHP to set the proper response header (content type and charset):

header('Content-type: application/json; charset=UTF-8');

You should also repeat this at the top of HTML pages:

<meta http-equiv="Content-type" value="text/html; charset=UTF-8" />

See also:

PHP UTF-8 cheatsheet

Ahsan Shah
  • 3,931
  • 1
  • 34
  • 48
1

PHP

try {
    $dbh = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);  
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $dbh->query('SET NAMES utf8;');
    $stmt = $dbh->prepare($sql);  
    //$stmt->bindParam("id", $_GET[id]);
    $stmt->execute();

    $advice = $stmt->fetchAll(PDO::FETCH_OBJ);
    $dbh = null;
    echo '{"items":'. json_encode($advice) .'}'; 
} catch(PDOException $e) {
    echo '{"error":{"text":'. $e->getMessage() .'}}'; 
}

Ajax

 var temp;
    $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: serviceurl,
            data: "{'userName':'" + userName + "' , 'password': '" + password                                   
                   + "'}",
            dataType: "json",
            success: function(msg) {
                            temp = jQuery.parseJSON(msg.d);
                          },
            error: function(xhr, ajaxOptions, thrownError) {}

        });
ridvanzoro
  • 646
  • 1
  • 10
  • 25
0

data.php

header('Content-type: application/json'); 

and

$.ajax({
        url: 'data.php',
        dataType: 'json',
        success: function(response) {
            jsonData = response;
        }
});
Jorge Y. C. Rodriguez
  • 3,394
  • 5
  • 38
  • 61