I have some understanding on cross domain calls and want to confirm them.
1. The cross-domain calls can be made in JQuery only when we set the datatype as "jasonp" or by using COR? What if the calling service is not returning the data in JSON format?
2. The ajax COR request code I have pasted below is correct or am I miss something?
- //serviceURL is the actual external URL
- var deffered = $.ajax({
- url: serviceURL,
- type: "POST",
- async: true,
- crossDomain: true,
- contentType: "text/plain",
- context: tdObject,
- //dataType: "jsonp",
- origin:window.location.protocol + "//" + window.location.host,
- timeout:5000,
- beforeSend: function ( xhr ) {
- xhr.setRequestHeader('Content-Type', 'text/plain');
- xhr.setRequestHeader('Access-Control-Allow-Methods', 'POST,GET,PUT, DELETE, OPTIONS');
- //xhr.setRequestHeader('Access-Control-Request-Headers', 'X-Requested-With');
- xhr.setRequestHeader('Access-Control-Allow-Origin','http://mymachine.com');
- xhr.setRequestHeader('Access-Control-Allow-Headers','Content-Type,X-Requested-With');
- xhr.withCredentials = true;
- },
- statusCode:{
- 0: function() {
- console.debug("0 for ");
- },
- 502: function(){
- console.debug("----> 502");
- }
- }
- /*success: function(data){
- //console.log("SUCCESS for ajax call ");
- httpStatusCode=data.responseCode;
- //return data.responseCode;
- },
- error:function(jqXHR, textStatus, errorThrown){
- //console.log("Error for ajax call "+jqXHR.status+"-- "+textStatus+"--"+errorThrown);
- httpStatusCode=jqXHR.status;
- //return jqXHR.status;
- }*/
- });
3. I have been told by the jquery experts that when i use JSONP as datatype, then i cannot get exact HTTP response code for the HTTP request I am sending. Say if the HTTP call is failing due to HTTP error code 404 or 500, then I want to display that. How to do this?
-Subbu.