PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, February 3, 2022

[FIXED] jQuery AJAX parses HTML response as JSON

 February 03, 2022     cakephp, html, jquery, json     No comments   

Issue

I cant find anything on the web about this mysterious bug. I wrote a simple Ajax calling a CakePHP Controller-Function to render a simple View. Finally i want to put that rendered HTML into a Popup:

The Ajax:

$.ajax({
    url: $('base').attr('href') + '/myController/renderPopupContent/' + this.view,
    type: "GET",
    dataType: "html",
    context: this,
    success: function( data ) {
        this.content = data;
        this.show();
    },
    error: function(xhr, status) {
        showMessage(status, xhr);
    }
});

Now i get a jQuery-Error which says:

Uncaught SyntaxError: Unexpected token <

This is because jQuery tries to (automatically^^) parse the response as JSON. If i debug the script it breaks at jQuery (1.9.1) at Line 541 and it really tries to parse that html-response (STRING!) into/from JSON.

How can i avoid this and get jquery known of the dataType "HTML"

Additional Info:

The jQuery-Error (@Ln541) occures after an "alert();" in my success-callback, so the Ajax is already done when the error is thrown.


Solution

Somewhere in another JS-File i found this snippet:

$(document).ajaxSuccess(function(evt, xhr, options) {
    var response = $.parseJSON(xhr['responseText']);

This obviously fires after every ajax and tries parsing the response.



Answered By - Jonathan
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing