Murphy1976
Murphy1976

Reputation: 1475

jQuery Ajax - Failed Resource or Uncaught Error

I'm running jQuery 1.9.1

On certain links in a Digital Library, I have the following code:

<a class="removeLink" onclick="getDigitalLibrary('removeFromLibrary.action?itemId=1000122007&searchType=ALL')">

Which calls THIS ajax call:

function getDigitalLibrary(urltoremove) {

    $.ajax({
        url: 'ecom/'+urltoremove,       
        async: false,
        success: function(data) {           
            $("#ajaxrequest").html($(data).find('#ajaxrequest').html());
        },
        error: function(data){
            alert("An error occurred");
        }
    });

    return remaining;
}

Sometimes when I load the page, I get this error:

 Failed to load resource: net::ERR_CACHE_MISS

And whenever I click the link, the entire source code is returned as a

Uncaught Error: Syntax error, unrecognized expression: <!doctype html>...

What am I missing here? If I simply put the full link that is supposed to be created with the Ajax call into the URL bar of my browser.. it works fine: it reloads the page and removes the item from the library.

Upvotes: 0

Views: 523

Answers (1)

phts
phts

Reputation: 3925

Use jQuery.parseHTML(data) to parse data and then pass it to $():

success: function(data) {           
    $("#ajaxrequest").html($($.parseHTML(data)).find('#ajaxrequest').html());
},

In your case $(data) supposes that data is a selector string. parseHTML will make data be supposed as HTML code.

http://jquery.com/upgrade-guide/1.9/#jquery-htmlstring-versus-jquery-selectorstring

Upvotes: 1

Related Questions