6

jQuery:

$.ajax({
url : url,
type : 'GET',
dataType: 'json',
data: {
    'FN'    : 'GetPages',
    'PIN'   : '7659' 
},
xhrFields: {
   withCredentials: true
},
crossDomain: true,
success: function(data) {
    alert('succsess');
    console.log('data', data);
},
error: function (xhr, ajaxOptions, thrownError) {
    alert('error');
    console.log(xhr.status);
    console.log(thrownError);
}
});

Firebug Firefox Network

Firebug Error

What happens

The AJAX "error:" event gets triggered and my console.log outputs are:

xhr.status -> 0

thrownError -> (empty String)

Is this normal? When I type the URL in a browser I receive a file download with the JSON content in it, this shouldn't be a problem right?

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
user1841515
  • 201
  • 1
  • 2
  • 8
  • Over a browser url the response is: [{"pg":0,"descr":"PC1"},{"pg":1,"descr":"PC2"},{"pg":2,"descr":"PC3"},{"pg":3,"descr":"HG1"},{"pg":4,"descr":"HG2"},{"pg":5,"descr":"HG3"},{"pg":6,"descr":"HG4"},{"pg":7,"descr":"DW1"},{"pg":8,"descr":"DW2"},{"pg":9,"descr":"CMN"}] – user1841515 Nov 23 '12 at 08:06
  • Also my understanding is that **jsonp** won't work caus I am not permitted to change anything on the server and its response will stay in json format and not jsonp format – user1841515 Nov 23 '12 at 08:10
  • jsonp wont affect the server. But it can help to get around CORS issues. It may be your only option without modifying the server – Jon Wells Nov 23 '12 at 08:16
  • @CrimsonChin ill have a look at that thanks! – user1841515 Nov 23 '12 at 08:50
  • @CrimsonChin only thing i managed to achieve was: 501 OPTIONS not implemented.. – user1841515 Nov 23 '12 at 09:12

3 Answers3

10

Thanks to @CrimsonChin I know its a Same Origin Policy problem

In computing, the same origin policy is an important security concept for a number of browser-side programming languages, such as JavaScript. The policy permits scripts running on pages originating from the same site to access each other's methods and properties with no specific restrictions, but prevents access to most methods and properties across pages on different sites.[1]

(from http://en.wikipedia.org/wiki/Same_origin_policy)

Granting JavaScript clients basic access to your resources simply requires adding one HTTP response header, namely:

Access-Control-Allow-Origin: *
Access-Control-Allow-Origin: http://foo.example.com

(from http://enable-cors.org/)

Ofc, turning the JSON response into a JSONP response would also work. Thx @djakapm

informatik01
  • 16,038
  • 10
  • 74
  • 104
user1841515
  • 201
  • 1
  • 2
  • 8
3

Try this one

$.ajax({ type : "GET", 
             url : URL, 
             data: {
             'FN'    : 'GetPages',
             'PIN'   : '7659' 
             },
            xhrFields: {
             withCredentials: true
             },
             crossDomain: true,
             dataType : "jsonp", 
             jsonp : "jsoncallback", 
             jsonpCallback : "SMS", 
             cache : true, 
              success : function(service_data) { 


                      },
              error : function(msg) {
                 alert(JSON.stringify(msg));
                }
          });
Jon Wells
  • 4,191
  • 9
  • 40
  • 69
gurusai
  • 153
  • 4
  • 16
  • I runned this code and got following alert box: {"readyState":4,"status":200,"statusText":"success"} And in firebug console an error: SyntaxError: illegal character in the **json** response "descr":"CMN"}]� <-- and a arrow pointing at that Questionmark that shouldnt be there!? – user1841515 Nov 23 '12 at 09:55
  • @user1841515 try running the service_data through json lint (http://jsonlint.com/) to make sure the json is well formatted – Jon Wells Nov 23 '12 at 10:00
  • @CrimsonChin I copied the json and run a check -> Valid JSON – user1841515 Nov 23 '12 at 10:07
  • @CrimsonChin The response isnt valid jsonp – user1841515 Nov 23 '12 at 10:23
  • Is this normal? When i type the url in a Browser i receive a file download with the json content in it, this shouldnt be a problem right? – user1841515 Nov 23 '12 at 10:30
2

I cant figured out from the image, but if you are trying send AJAX request to different domain, you need to use JSONP, simple AJAX request will not be sufficient

Try to change dataType: 'json' to dataType: 'jsonp'

and add callback=? to your URL

informatik01
  • 16,038
  • 10
  • 74
  • 104
djakapm
  • 175
  • 7
  • Ok so i tried it out and i received following error: Syntax Error descr":"DW1"},{"pg":8,"descr":"DW2"},{"pg":9,"descr":"CMN"}] – user1841515 Nov 23 '12 at 08:26
  • 1
    And Error: jQuery172046435253250156217_1353659110552 was not called – user1841515 Nov 23 '12 at 08:27
  • Do you have the control over the server response? in php I usually give response like this: $_GET['callback'].'()'. it will mimic a javascript function call – djakapm Nov 23 '12 at 08:30
  • @ djakapm No im afraid not. But why do i get a response when i type it in url of browser? – user1841515 Nov 23 '12 at 08:33
  • 2
    @user1841515 you dont violate CORS rules by accessing the url directly. I honestly think you're only option is to update the servers web.config to allow CORS – Jon Wells Nov 23 '12 at 10:12