XMLHttpRequest and response encoding

XMLHttpRequest and response encoding

daniel.wrzesniewski
Participant Participant
684 Views
2 Replies
Message 1 of 3

XMLHttpRequest and response encoding

daniel.wrzesniewski
Participant
Participant

I use XMLHttpRequest in an action script to get some informations from my tenant.

Everything works fine but I have problem with response encoding. 

It is piece of the json  

"description":"Wrześniewski, Daniel","details":{"descriptor":"Wrześniewski,

How can I set  the correct encoding? 

0 Likes
Accepted solutions (1)
685 Views
2 Replies
Replies (2)
Message 2 of 3

philipfrench
Collaborator
Collaborator
Accepted solution

I tried the script below, and made this example work.

The intention is to run a GET on this item, and copy the Description into Result.

[This is absolutely NOT the way to write a script more generally, and interrogating a tenant via API is absolutely the last resort, not the first resort, but anyway...]

 

At line 5 you can see the charset is specified in the Accept header.

The result is what we want...

philipfrench_0-1726160726983.png

 

If you don't include the charset in the Accept, you get a string encoded into whatever charset the server thinks you deserve, and when this it written to a field expecting utf8, you get a mess...

philipfrench_1-1726160759213.png

I suspect this is what you are seeing.

Try including the charset in the Accept...

 

 

 

var req = new XMLHttpRequest();
req.open("GET", tenantUrl + '/api/v3/workspaces/'+item.master.workspaceID+'/items/'+item.master.dmsID , false);  
prepareRequest(req);

req.setRequestHeader('Accept', 'application/json;charset=UTF-8');

req.send();

var jsonObj = JSON.parse( req.responseText );

for (var i in jsonObj.sections) {
    var section = jsonObj.sections[i];
    for (var j in section.fields) {
        var field = section.fields[j];
        if (field.title == 'Description') {
            item.RESULT = field.value+' at '+new Date();
        }
    }
}

...

 

0 Likes
Message 3 of 3

daniel.wrzesniewski
Participant
Participant

Thank you, Philip.

Setting the request header helped. 

0 Likes