Jump to content

.ajax() parsererror / crossdomain issue


viviosoft

Recommended Posts

Hello guys!

 

My problem seems simple but not.  I've been struggling with this problem for some time now.  I've passed the first part (I think) of getting data from my remote server.  However, the error I'm getting is parsererror when getting the data back in the ajax call?

 

The jQuery (1.7.1) source code make the request:

 

        $.ajax({
            type: "GET",
            url: 'http://www.domain.com/projects/vs/mobile/services/android-stores-list.php',
            contentType: "application/json",
            dataType: "jsonp",
            crossDomain: true,
            beforeSend: function(xhr) {
                alert('before send test');
            },
            error: function(jqXHR, textStatus, errorThrown) {
                alert('Sorry. Error during parsing Sport Data JSON!-> ' + textStatus);
            },
            success: function(data, status) {
                console.log(status + ' jSON load!' + data);
            },
            complete: function(obj, textStatus) {
                alert('complete: ' + textStatus);
            }
        });

 

Here's the php file that gets the data android-stores-list.php: (I realize that I'm posting this in Ajax help, wasn't sure the best place to get help?)

 

header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('content-type: application/json; charset=utf-8');

try {

    $dbh = new PDO("mysql:host=localhost;dbname=$db_name", $db_user, $db_pass);
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $stmt = $dbh->prepare($sql);
    //	$stmt->bindParam("id", $_GET[id]);
    $stmt->execute();
    $stores = $stmt->fetchAll(PDO::FETCH_OBJ);
    $dbh = null;
    echo '{"items":' . json_encode($stores) . '}';

} catch (PDOException $e) {
    echo '{"error":{"text":' . $e->getMessage() . '}}';
}

 

The json response looks like the following:

 

{"items":[{"id":"29","cId":"3","sName":"Milford Plaza Hotel","sNumber":"3061","sCustNm":"VS7997","sSalesman":"1234","sAddress":"","sContact":"","sPhone":""},{"id":"26","cId":"7","sName":"Circle K Bowling Green","sNumber":"5670","sCustNm":"VS7976","sSalesman":"1234","sAddress":"1091 N Main Street\r\nBowling Green, Ohio 43402","sContact":"","sPhone":""}]}

 

 

Any direction would be greatly welcome!  Thanks for any help you can provide me.

 

Link to comment
https://forums.phpfreaks.com/topic/261386-ajax-parsererror-crossdomain-issue/
Share on other sites

I'm not familar with jquery too much, but my understanding from reading the docs is that you'd need your server-side script to look for the $_GET['callback'] parameter and then output that as a function to call with the json as an argument.

 

Eg.

 

<?php
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('content-type: application/json; charset=utf-8');

$json = '';

try {

    $dbh = new PDO("mysql:host=localhost;dbname=$db_name", $db_user, $db_pass);
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $stmt = $dbh->prepare($sql);
    //	$stmt->bindParam("id", $_GET[id]);
    $stmt->execute();
    $stores = $stmt->fetchAll(PDO::FETCH_OBJ);
    $dbh = null;
    $json = '{"items":' . json_encode($stores) . '}';

} catch (PDOException $e) {
    $json =  '{"error":{"text":' . $e->getMessage() . '}}';
}

echo $_GET['callback'].'('.$json.');';

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.