0

I am trying to get data from a server using JSONP but I keep getting a warning in my console

Resource interpreted as Script but transferred with MIME type text/json:

Below is my ajax call

var loginData = {
    "strUniqueID" : "123",
    "UserName" : "UserName",
    "Password" : "Password"
};

$.ajax({
    url: http://domain.com,
    type: "POST",
    data: {'data':$.toJSON(loginData)},
    contentType: "text/plain",
    dataType: "jsonp",
    jsonpCallback: 'jsonCallback',
    success: function(data) {
        console.log('Success');
        console.log(data);
    },
    error: function(jqXmlHttpRequest, textStatus, errorThrown) {
        // Display the error.
        console.log('Error: ' + textStatus);
        console.log('Error thrown: ' + errorThrown);
    }
});

Because of the warning I am receiving I am also getting this error

Uncaught SyntaxError: Unexpected token : 

In Firefox this shows up as

SyntaxError: missing ; before statement

Is this because my data isn't getting brought back in the correct format?

If I open the URL with the parameters I get the response correctly

{"response":"SESSION ID"}

But I also get an error from the ajax call

Error: parsererror
Error thrown: jsonCallback was not called

I've been racking my brains with this for a few hours now and I am stumped!

Pooshonk
  • 1,284
  • 2
  • 22
  • 49
  • Do you actually have a function in your page called `jsonCallback`? If not, then that's one problem. You either don't specify it and let jQuery create one for you, or you do specify it but you have to actually create it exactly with the same name. Judging by the response you get, it doesn't seem like you need to specify one. I would remove that parameter completely from the request. – Icarus Apr 03 '14 at 14:50
  • No I don't, I don't understand if I need it or not. What would I do when I call that function? – Pooshonk Apr 03 '14 at 14:54
  • Nothing. You have a success handler already defined. jQuery will redirect it to the success handler. – Icarus Apr 03 '14 at 15:00

1 Answers1

0

I found another question which helped me.

I needed to allow JSONP request on the service to send back the correct response

Community
  • 1
  • 1
Pooshonk
  • 1,284
  • 2
  • 22
  • 49
  • I am surprised that you didn't see the error before. You should have seen `No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://yoursite.com' is therefore not allowed access.` That would have been an indication that you had to enable CORS in the first place. – Icarus Apr 03 '14 at 15:25
  • I switched to JSONP a while back and totally forgot about it. I have tried so many combinations, i'm just happy its finally working – Pooshonk Apr 03 '14 at 15:42