Caner Akdeniz
Caner Akdeniz

Reputation: 1862

passing headers with ajax in jquery

I have a problem with passing headers in ajax call on jquery.

$.ajax({
    url: '/resources/ajax/customize.aspx?' + qs + '&nocache=' + Math.random(),
    contentType: "application/json",
    headers: values,
    context: $this,
    cache: false,
    success: function(data) {
          //do stuff here
    }
});

In most cases, it gets the headers values right, but sometimes it doesn't get any headers value. I made sure that 'values' variable contains data. I was wondering is there any specific cases that headers don't pass in ajax?

Update: I tried it as:

 $.ajax({
     url: '/resources/ajax/customize.aspx?' + qs + '&nocache=' + Math.random(),
     contentType: "application/json",
     beforeSend: function(xhr) { xhr.setRequestHeader('values',values); },
     //headers: values,
     context: $this,
     cache: false,
     success: function(data) {
                  //do stuff here
     }
 });

and there is no luck with that.

Update 2

Figured out the problem. There was a URL variable with line breaks in it, so line breaks caused headers not to be passed.

Upvotes: 0

Views: 1296

Answers (1)

saurshaz
saurshaz

Reputation: 509

I was facing the same issue. Got rid of the line breaks by using jquery's(or do it in JS otherwise) $.trim() method. In my case I knew that \n\r won't be a part of the value legally. this has to be checked for trim to fix it.

Upvotes: 1

Related Questions