-1

I've trying to build profanity filtering api. The response of api is coming in JSON. I'm using following codes to access the api. But, it's showing some error which i can't able to fig out.

Here is my code..

 $.ajax({
           dataType: 'jsonp',                   
           jsonp: 'callback',
           url: 'http://www.employeeexperts.com/Profanity/index.php/rest/check/Good Morning',                       
           success: function(data) {
               console.log('success');
               console.log(JSON.stringify(data));
            },
            error: function (header, status, error) {
                console.log('ajax answer post returned error ' + header + ' ' + status + ' ' + error);
            }
        });

Regards

Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
Suresh
  • 5,687
  • 12
  • 51
  • 80
  • The target application doesnot seems to be supporting jsonp – Arun P Johny Aug 21 '13 at 05:33
  • @ArunPJohny What you mean by 'doesnot seems to be supporting jsonp'. Coz, if u only execute the url you can see it's returning JOSN string. – Suresh Aug 21 '13 at 05:37
  • what is the whole point, if jsonp is supported will won't return a simple `json` it returns it by wrapping it in a function call, whose name will be specified via the `callback` parameter – Arun P Johny Aug 21 '13 at 05:39
  • Do u mean , rather than returning like this... {"status":"false","orig":"Good Morning","clean":"Good Morning"} it should be like this.. "status":"false","orig":"Good Morning","clean":"Good Morning" – Suresh Aug 21 '13 at 05:42
  • it should be `callbakName({"status":"false","orig":"Good Morning","clean":"Good Morning"} )` – Arun P Johny Aug 21 '13 at 05:43
  • Hey @ArunPJohny, then how my $.ajax() should be. Can u modify my $.ajax code – Suresh Aug 21 '13 at 05:49
  • bad luck... it cannot be done using client side javascript... since the server is not supporting jsonp or CORS... you need to use some kind of serverside proxying to achieve this – Arun P Johny Aug 21 '13 at 05:52
  • @ArunPJohny Now, i've added the callback also in json string. So, can u plz help me how should my $.ajax() should be. So that, i can catch those response string. – Suresh Aug 21 '13 at 06:03
  • @mi6crazyheart the name of the function should not be hardcoded, it should come from a request parameter called `callback`, so if the parameter is `callback=xyz` then the response should be `xyz({...})` – Arun P Johny Aug 21 '13 at 06:10
  • @mi6crazyheart also if the parameter `callback` is not present or empty then you need to return just the json without the wrapper – Arun P Johny Aug 21 '13 at 06:11
  • @ArunPJohny Can u tell me one thing.. what should be my $.ajax() code with JSON response so that it'll work accordingly. – Suresh Aug 21 '13 at 06:25

1 Answers1

0

Your JSON response is not wrapped inside a call back function.

your response should be something like this

callback(
{
  "status": "false",
  "orig": "Good Morning'",
  "clean": "Good Morning'"
});

callback should be a js function on your page which will be fired. When you use jQuery this is done automatically but you still have to wrap your server response with the callback function which is passed as a query parameter.

here is a sample server url with JSONP response

http://api.flickr.com/services/feeds/photos_public.gne?format=json&jsoncallback=callback

Kishore
  • 1,914
  • 1
  • 15
  • 22