1

It is my first time using JSON. I am trying to get JSON data from a php script using ajax.But i am getting this error "Error.Parsing JSON Request failed" "undefined".Help me this is my php script test.php

    $data='{
  "one": "One",
 "two": "Two",
 "three": "Three"
   }';

 header('Content-type: application/json');
 echo json_encode($data);

and here i am getting the data getdata.php

    var sURL = 'http://www.example.com/test.php';   

$.ajax({
            type: "GET",
                            url:sURL,
                            dataType:"jsonp",
                            crossDomain:true,
                            data: {transid:trans_id , bookingdate: dateVal, bookingtime: timeVal, People: peopleVal,affiliateid: affiliate },
                            async: false,
                            contentType:"application/json; charset=utf-8",
                            success: function (data){
                                                                                    var result = JSON.parse(data);
                                            alert(result);
                                            },
                                            error: function (x, e) {

        if (x.status == 0) {
            alert(x.response);
            alert(x + " " + e);
            alert('You are offline!!\n Please Check Your Network.');
        }
        else if (x.status == 404) {
            alert('Requested URL not found.');
        }
        else if (x.status == 500) {
            alert('Internel Server Error.');
        }
        else if (e == 'parsererror') {

            alert('Error.\nParsing JSON Request failed.' + e.statusText);
            alert(x.response);
        } else if (e == 'timeout') {
            alert('Request Time out.');
        } else {
            alert('Unknow Error.\n' + x.responseText);
        }
    }

                            });

This is my first question so excuse any mistakes

2 Answers 2

2

As you are using jsonp, add callback function as below

var sURL = 'http://www.example.com/test.php';   

$.ajax({
            type: "GET",
                            url:sURL,
                            dataType:"jsonp",
                            jsonp : "callback",
                            jsonpCallback: "jsonpcallback",
                            crossDomain:true,
                            data: {transid:trans_id , bookingdate: dateVal, bookingtime: timeVal, People: peopleVal,affiliateid: affiliate },
                            async: false,
                            contentType:"application/json; charset=utf-8",
                            success: function (data){
                                                                                    var result = JSON.parse(data);
                                            alert(result);
                                            },
                                            error: function (x, e) {

        if (x.status == 0) {
            alert(x.response);
            alert(x + " " + e);
            alert('You are offline!!\n Please Check Your Network.');
        }
        else if (x.status == 404) {
            alert('Requested URL not found.');
        }
        else if (x.status == 500) {
            alert('Internel Server Error.');
        }
        else if (e == 'parsererror') {

            alert('Error.\nParsing JSON Request failed.' + e.statusText);
            alert(x.response);
        } else if (e == 'timeout') {
            alert('Request Time out.');
        } else {
            alert('Unknow Error.\n' + x.responseText);
        }
    }

 });

function jsonpcallback(rtndata) {
  alert(rtndata.one);
}

In PHP make $data as array and then use json_encode() with return callback.

 $data=array(
  "one"=>"One",
  "two"=>"Two",
  "three"=>"Three"
 );
 header('Content-type: application/json');
 echo $_GET['callback']. '('. json_encode($data) . ')';  
Sign up to request clarification or add additional context in comments.

5 Comments

didnt help.now fire bug is also giving a error of SyntaxError: invalid label [Break On This Error] {"one":"One","two":"Two","three":"Three"}(line 1, col 1)
@KaranTuteja: try myanswer combined with this answer, it should work as json_encode method accepts array only..!!
now its giving this error in firebug SyntaxError: JSON.parse: unexpected character [Break On This Error] var result = JSON.parse(data);
are you sure want to get json from cross-domain ?
@gbd yes i need cross domain it is very important for my application.
0

Change your

     dataType:"jsonp",

to

dataType:"json",

2 Comments

tried it didnt help. it doesn't effect anything anyways jsonp is used for cross domain.
what do you mean by crossdomain(on same proxy)? or from external source?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.