Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery - ajax request specifying content type as html

Tags:

jquery

ajax

I'm using the modernish jquery 1.5 ajax request. To a page on the same domain. I'm trying to improve upon behaviour in an intranet site using grease monkey and jquery.

var jqxhr = $.ajax("anotherpage-response.html")
.success(function(data) {alert("cmp"); console.log(data);})
.error(function() { alert("error"); })
.complete(function() { alert("complete"); });

Its currently being returned as a string. Any idea how I can get it returned as a dom like object which I can proccess using jquery selectors ???

I think im after something like fileType: html but it doesnt seem to be an option in the ajax request. Maybe I just need to read the api properly ???

Thanks

like image 719
wmitchell Avatar asked Nov 09 '11 15:11

wmitchell


2 Answers

You should read up on jquery's documentation on this subject: http://api.jquery.com/jQuery.ajax/ The thing you are looking for probably is the following: dataType.

$.ajax({ 
    url: 'request.html', 
    succes: function(data){ 
        .. do something here! ..
    }, 
    dataType: 'html'
});

I hope this helps

like image 86
Stijn_d Avatar answered Sep 28 '22 16:09

Stijn_d


No matter what you will get back a string as plain text.

From the jQuery docs on dataType:

""html": Returns HTML as plain text; included script tags are evaluated when inserted in the DOM."

You can always turn plaintext into a dom object though..

$.ajax({ 
url: 'request.html', 
succes: function(data){ 
  $(data) // This is now (kind of) a DOM object that you can use jQuery selectors on
}, 
dataType: 'html'
});
like image 31
CambridgeMike Avatar answered Sep 28 '22 16:09

CambridgeMike