Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to pass headers in jQuery.load like ajax?

I've been stuck from this issue in couple of days. I want to send some headers data in jQuery.load(). It seems that jQuery.load never send the headers, like ajax. Can somebody explain how to, or Is it necessary? Btw, sory my bad English.

This is the syntax :

$loadingBay.load(href, settings.data, function (data, status) {
    prep(status === 'error' ? $tag(div, 'Error').html(settings.xhrError) : $(this).contents());
});

Many Thanks

like image 410
Rocky Andra Avatar asked Nov 19 '13 08:11

Rocky Andra


People also ask

How do I get a header from ajax request?

Whenever an Ajax request is about to be sent, our FileExplorer triggers the beforeAjaxRequest event. Using this event, you can add the custom header in Ajax Request of FileExplorer. // Using “beforeSend” event, you can specify your custom method.

What is ajax content type?

contentType is the type of data you're sending, so application/json; charset=utf-8 is a common one, as is application/x-www-form-urlencoded; charset=UTF-8 , which is the default. dataType is what you're expecting back from the server: json , html , text , etc.


1 Answers

You can not pass headers data to $.load() , but you can set default setup using $.ajaxSetup() like this :

$.ajaxSetup({
    'headers':{
        'header1':'value1',
        'header2':'value2',
    }
}
);

//give the load call here
$('selector').load('url',function(){
    //do things
})

Disclaimer From jquery doc:

Its use is not recommended.

The best way is do the is thing using $.ajax() :

$.ajax({
  url: "test.html",
  headers : {header1 : "header1"}       
  }).done(function(data) {
     $('selector').html(data);
});
like image 176
MD. Sahib Bin Mahboob Avatar answered Oct 26 '22 12:10

MD. Sahib Bin Mahboob