-1

While trying to use:

$.ajax({
    type: "POST",
    url: surl,
    data: {funcToCall: "getHello",val:"Luke"},
    dataType: "jsonp",
    jsonp : "callback",
    jsonpCallback: "getHelloResponse"

}).success(function(data){
    alert(JSON.stringify(data));
}).
    error( function (data){
    alert(JSON.stringify(data));
});

I receive:

{"readyState":4,"status":200,"statusText":"success"}

But when leaving out the type which defaults type to "GET", it works fine!

The target php page was edited to handle POST and GET.

EDIT:

So the actual question is: why is the ajax post not returning the expected result of "Hello Luke!"?

Rizier123
  • 58,877
  • 16
  • 101
  • 156
Luke
  • 776
  • 9
  • 24
  • 1
    You don't like http status code 200 `OK`? – PeeHaa Aug 15 '12 at 10:51
  • 6
    I don't think using POST request alongside JSONP is a good idea. See, JSONP request is essentially ` – raina77ow Aug 15 '12 at 11:56
  • @LukeLindner Should I make this an answer for you to accept? ) Technically it's not an answer to your question, though; I don't know why `data` becomes `jqXhr` object. – raina77ow Aug 15 '12 at 14:39
  • @raina77ow Its up to you, but you did answer my question: don't use POST with jsonp. – Luke Aug 15 '12 at 19:44

1 Answers1

0

The success and error are expected as $.ajax settings, which return jqXhr, on which you can execute .done() for example, but not success or error.

Here is the working variant:

$.ajax({
    type: "POST",
    url: surl,
    data: {funcToCall: "getHello",val:"Luke"},
    dataType: "jsonp",
    jsonp : "callback",
    jsonpCallback: "getHelloResponse",
    success: function(data){
        alert(JSON.stringify(data));
    }
    error: function (data){
        alert(JSON.stringify(data));
    }
});

This is done example, where if you can check if the execution is passed normally or something went wrong.. etc.

$.ajax({
    url: "file.php",
    context: document.body
}).done(function() { 
    ($this).addClass("greenBody"); // This is the context - document.body
});
Rolice
  • 3,063
  • 2
  • 24
  • 32