2

I have really strange problem with ajax json response. The data in the json response (jqXHR.responseJSON) does not match the string response data (jqXHR.responseText). Request is without any errors (returning status code 200 OK) with json string data:

{"pdata":{"1":{"x":158,"y":545},"2":{"x":259,"y":179},"3":{"x":503,"y":77},"4":{"x":435,"y":528}},"status":"0","result":true}

I have simle code for console logging:

    var loadItemData = function(image) {
        var data = {};
        // Prepare request data
        data.image = image;
        data.token = $this.getOpt('token');
        // Call image get data url
        $.ajax({
            url: $this.getOpt('image_get_data_url'),
            type: 'post',
            data: data,
            dataType: 'json',
            success: function(json, textStatus, jqXHR) {
                console.log(jqXHR.responseText);
                console.log(jqXHR.responseJSON);
                console.log($.parseJSON(jqXHR.responseText));
            },
            error: function(xhr, ajaxOptions, thrownError) {
                alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
            }
        });
    };

The console output from ajax success callback above is: enter image description here The question is, why responseJSON has different data as responseText and why manually parsed responseText giving right data output? I noticed this problem after deploying changes on test server and don't have this problem on localhost (WAMP server). Using Firefox 66.0.5 (32-bit). Its developer tool showing correct data in Network tab for this request.

user1827257
  • 1,600
  • 4
  • 17
  • 24
  • 2
    Did you specify `dataType: "json"` in the `$.ajax` call? – Barmar May 14 '19 at 08:09
  • 1
    It's very strange that the first level of the object has all the correct data, it's only the nested values that are wrong. I don't see how that can happen. – Barmar May 14 '19 at 08:13
  • 1
    Either it should parse the whole response or not, it shouldn't parse it but leave some properties set to `0`. – Barmar May 14 '19 at 08:14
  • Can you show your ajax command please? – Marcus May 14 '19 at 08:16
  • @Marcus whole ajax call definition added – user1827257 May 14 '19 at 08:22
  • 1
    Why dont you just use the first parameter of the `success` callback? `json` in above case. – Chay22 May 14 '19 at 08:25
  • 1
    Like @Barmar said I don't see how this is possible. My only thought is that this is possibly a caching bug within jQuery, where one property is read from cache and the other is not. Try adding `cache: false` to the request - although to be honest I'm grasping at straws to explain this behaviour. – Rory McCrossan May 14 '19 at 08:29
  • @Chay22 totally the same values as in `jqXHR.responseJSON`. I think it's the same object anyway, isn't it? – user1827257 May 14 '19 at 08:30
  • @RoryMcCrossan POST is never cached. – Barmar May 14 '19 at 08:30
  • @Barmar yep, you're right. I have absolutely no idea how this is possible then. OP, is it possible for you to recreate this behaviour for us to see in a fiddle/snippet? – Rory McCrossan May 14 '19 at 08:31
  • Caching isn't done by jQuery, it's done by the browser. All that `cache: false` does is add a cachebuster to the URL, it doesn't change how jQuery processes it. – Barmar May 14 '19 at 08:32
  • 1
    Is there any code that modifies `responseJSON`? The behavior you're seeing is usually because the console shows a live version of the object. If you expand a field after it has been modified, you'll see the updated value. – Barmar May 14 '19 at 08:33
  • @RoryMcCrossan @Barmar Hm, I feel little bit embarrassed now. Yes, there WAS code that was modifying data like `json.pdata = $this.getDisplayPoints(json.pdata)` and returning wrong values (zero-s). BUT, why logging show those zero-s even i remove all modification code and left only those 3 lines for debugging? Some kind of cache? – user1827257 May 14 '19 at 09:01
  • It shouldn't happen if you removed the modification code. I think you must not really have removed it. – Barmar May 15 '19 at 19:20

0 Answers0