0

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.

Caner Akdeniz
  • 1,862
  • 14
  • 20
  • 1
    Why are you passing a `nocache` parameter if you're already adding `cache: false`? – Blender Mar 12 '13 at 20:20
  • 1
    What headers are you passing on? Is it browser-specific? Maby http://stackoverflow.com/questions/7433556/jquery-jsonp-ajax-authentication-header-not-being-set helps? – ivodvb Mar 12 '13 at 20:21
  • probably a duplicate of [this](http://stackoverflow.com/questions/3258645/pass-request-headers-in-a-jquery-ajax-get-call) question? – Mike Corcoran Mar 12 '13 at 20:23
  • 2
    There is list of headers that can't be set or changed using ajax call http://stackoverflow.com/questions/5077998/how-to-modify-cookie-from-ajax-call#5123282 – jcubic Mar 12 '13 at 20:24
  • @Blender just to prevent browser caching. – Caner Akdeniz Mar 12 '13 at 20:35
  • @IvovanBeek they are just the variables that i need to pass to do some stuff on back-end. – Caner Akdeniz Mar 12 '13 at 20:37
  • @jcubic i am not trying to set or change any browser headers, i am just passing my data through headers. In most cases it works properly. – Caner Akdeniz Mar 12 '13 at 20:38
  • @CanerAkdeniz: Show us the cases (i.e. the value of the `values` variable) where it does not work! – Bergi Mar 12 '13 at 21:16
  • These are the values i am passing in. Only thing changes from call to call is values of the tags Here is the image: http://tinypic.com/r/ev9r9c/6 – Caner Akdeniz Mar 12 '13 at 21:26

1 Answers1

1

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.

saurshaz
  • 489
  • 5
  • 17
  • 1
    Thanks for the comment, i have resolved it long time ago. You are right, it was all about the line breaks. As soon as you remove them, it worked fine. – Caner Akdeniz Sep 17 '13 at 14:24