0

I have a JSONP , I read go through some of the web , it said JSONP require a token , so i add product in front of my JSON , and i not sure is this a correct JSONP. correct if this format is wrong.

I am using json_encode this php funCtion to get this JSON string

product[{"Product_ID":"1","Product_Name":"Apple","Product_Description":"New","Product_Image":"http:\/\/www.abc.sda.jpg"},{"Product_ID":"2","Product_Name":"Microsoft","Product_Description":"Microsoft","Product_Image":"Microsoft"}]

My Ajax is as follow

    $.ajax({url: "http://www.someweb.com/testing.php",
        dataType: "jsonp",
        async: true,
        success: function (result) {
            alert("Success");
        },
        error: function (request,error) {
            alert('Network error has occurred please try again!');
        }
    });

With this call i am getting the error "unexpected token" , How do I pass in my token which is "product" so that i am able to retrieve my data?

abc cba
  • 2,563
  • 11
  • 29
  • 50
  • JSONP is just a function call wrapping a script tag. The token you're wrapping it in is just a function call. – Benjamin Gruenbaum Oct 20 '13 at 17:27
  • @BenjaminGruenbaum Do you mean my page should return something like – abc cba Oct 21 '13 at 15:16

2 Answers2

0

The format is:

functionName( json );

where functionName is (usually) specified by the client (typically through the callback query string parameter).

You need the paraenesis and should generally avoid hardcoding the function name.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • do you mean I have to wrap my generate JSONP in my php code into a function ? – abc cba Oct 20 '13 at 17:31
  • The output of the PHP script should be a JavaScript program consisting of a single function call with one argument (your JSON). How you produce that inside your PHP is entirely up to you. – Quentin Oct 20 '13 at 17:32
0

Pass your callback function embeded to your url.

http://www.someweb.com/testing.php?callback=CALLBACK_FUNCTION_NAME

Sajuna Fernando
  • 1,364
  • 9
  • 16
  • I cant see a callback function specified :S – Sajuna Fernando Oct 20 '13 at 17:31
  • It doesn't need to be specified. It is jQuery and has `dataType: "jsonp"` so it will be added by the library. – Quentin Oct 20 '13 at 17:31
  • do you mean I have to wrap my generate JSONP code in my php code into a function ? – abc cba Oct 20 '13 at 17:31
  • Nope as Quentin rightly pointed out when you specify JSONP jquery will automatically add the callback parameter.. Maybe you can have a look at this thread http://stackoverflow.com/questions/7936610/json-uncaught-syntaxerror-unexpected-token – Sajuna Fernando Oct 20 '13 at 17:32