I'm using JQuery ajax post with JSONP as datatype to resolve the problem of CrossDomain
this is my code implementation :
$.ajax({
statusCode: {
200: function() {
alert("page found");
},
302: function() {
alert("page redirect");
}
},
url: "http://exemple.com/j_spring_security_check",
type: "post",
dataType: 'jsonp',
jsonp: false,
jsonpCallback: 'localJsonpCallback',
contentType: 'application/json; charset=utf-8',
data: {ajax: true, username: 'user1', password: 'pass'},
success: function(){
alert("success");
},
error:function(jqxhr, textStatus, error){
alert("textStatus : " + textStatus + "\n error" + error);
}
});
function localJsonpCallback(data) {
alert("localJsonpCallback : " + data);
}
The problem is that I'm getting this message's error :
errorParsing , localJsonpCallback callback was not called.
I know why I'm getting this kind of message, because the client was attending a format like this :
localJsonpCallback({"success":1});
but I'm getting a result like that : {"success":1}
The problem is the callback method is not implemented in server because the guys know how to override this method with spring security. That's why it isn't called. And I don't have access to server.
Is there any way to get JSONP response without the implementation of callback function in the server ??
PS: I have to use JSONP to avoid this like error access-control-allow-origin not allowed
I resolved this issue in the server side, so when I call the web service I add an attribute with the name of the callback function I want to catch. and the web service must return a response like this FUNCTION_NAME({JSON_STRUCTURE})
.
So As I mentioned in the question, when you make a request. Put an attribute to be recognise by the WebService and to return it in response . For example, If I want to get the list of contacts, I do like this :
http://example.com/getContacts?callbackName=getcontactsCallback
the response must be like this:getcontactsCallback({name:john , job:developer})
and the same name of callback must be indicated in this attribute for the Ajax request :
jsonpCallback: 'localJsonpCallback',