2

I'm trying to call a rest service from jQuery using the code below

$.ajax({
    type: verb,
    url: url,
    headers: header,
    crossDomain: true,
    xhrFields: {
        withCredentials: true
    },
    contentType: 'application/json; charset=utf-8',
    data: JSON.stringify(data),
    dataType: 'text',
    accept: 'application/json charset=utf-8'
}).done(function (ResultData) {
    //do something
}).error(function (jqXHR, textStatus, errorThrown) {
   //do something
});

It works perfectly all the time, but when I passed an Arabic character in the header like this:

header["sFilter"]="عربي"

The following error raised:

DOMException: Failed to execute 'setRequestHeader' on 'XMLHttpRequest': 'عربي' is not a valid HTTP header field value.

I tried to add a beforeSend function to set "Content-Type" but nothing changed

 beforeSend: function(XMLHttpRequest){
               XMLHttpRequest.setRequestHeader("Content-Type", "text/plain;charset=UTF-8");                          
            },

Is there is a way to solve this issue without encoding the text in the client side and decoding it in the server side, as I don't have access to the server side code?

Heba Gomaah
  • 1,145
  • 4
  • 11
  • 16
  • HTTP headers don't support UTF8 values. Have you tried `encodeURIComponent("عربي")`? – haim770 Nov 23 '15 at 11:44
  • Is there a way to till the server that the header need to be decoded without changing the server side code? – Heba Gomaah Nov 23 '15 at 11:48
  • I have no clue about your server but I assume that *something* would have to be done. If not in the application code then in server or http pipeline configuration. – haim770 Nov 23 '15 at 11:49
  • do you have any suggestions about the something that may be done in the http pipeline configuration? – Heba Gomaah Nov 23 '15 at 11:52
  • @HebaGomaah — Probably, but it wouldn't do you any good since **the browser** is refusing to send the malformed header in the first place. – Quentin Nov 23 '15 at 11:53
  • [link](http://stackoverflow.com/questions/5423223/how-to-send-non-english-unicode-string-using-http-header) this says you can't without an encoding scheme – Ali Naci Erdem Nov 23 '15 at 11:55
  • @Quentin, I assume he asked about ways to *decode* the value after encoding it using `enodeURIComponent`. – haim770 Nov 23 '15 at 11:55
  • @HebaGomaah What is the web server? Any framework involved? – haim770 Nov 23 '15 at 11:57
  • I really have no idea, may be I need to talk to the system administrator. – Heba Gomaah Nov 23 '15 at 12:00

0 Answers0