I used XMLHttpRequest to POST something and want to get return of Chinese character with JSON format,
but I got unreadable character that should be Chinese UTF-8 character.
in server-side and ensure response Http-Header Content-type is UTF-8 format.
My Code is on below:
var xhr = new XMLHttpRequest();
var statusPostLabel = cc.LabelTTF.create("Status:", "Thonburi", 18);
this.addChild(statusPostLabel, 1);
statusPostLabel.setPosition(cc.p(winSize.width / 2, winSize.height - 160));
statusPostLabel.setString("Status: Send Post Request to httpbin.org");
xhr.open("POST","http://dualstack.mslb-393040804.ap-southeast-1.elb.amazonaws.com:8080/clientapi/getquestionbygroupid.do?version=1&request_gid=56");
// cc.log("header:"+xhr.getResponseHeader("Content-Type"));
xhr.onreadystatechange = function() {
cc.log("TEST POST: xhr.onreadystatechange..., status: " + xhr.status);
var httpStatus = xhr.statusText;
var response = xhr.responseText.substring(0,100) + "...";
cc.log("header:"+xhr.getResponseHeader("Content-Type"));
var responseLabel = cc.LabelTTF.create("POST Response (100 chars): \n" + response, "Thonburi", 16);
that.addChild(responseLabel, 1);
responseLabel.setPosition(cc.p(350, winSize.height - 220));
statusPostLabel.setString("Status: Got POST response! " + httpStatus);
}
xhr.send("test=ok");
I print string in MinXmlHttpRequest::handle_requestResponse(cocos2d::extension::CCHttpClient sender, cocos2d::extension::CCHttpResponseresponse) , the response result is correct(utf-8), but when transmit to Javascript-side, responseText are unreadable character.
maybe try BASE64 encode on your server
and decode in your JS.
Zax Ma,
Thanks for your reply.
To figure out my problem simply,
I send chinese character from XMLHttpRequest.cpp(C*+ side) directly as below:
<pre>
JS_BINDED_PROP_GET_IMPL
{
// JSString* str = JS_NewStringCopyZ.c_str);//, dataSize);
JSString* str = JS_NewStringCopyZ;//, dataSize);
if {
vp.set);
//JS_ReportError.c_str);
return JS_TRUE;
} else {
JS_ReportError;
return JS_FALSE;
}
}
</pre>
and XHRTest.js I wrote:
<pre>
sendPostRequest: function {
var that = this;
var xhr = new XMLHttpRequest;
var statusPostLabel = cc.LabelTTF.create;
this.addChild;
statusPostLabel.setPosition);
statusPostLabel.setString;
xhr.onreadystatechange = function {
cc.log;
var httpStatus = xhr.statusText;
var bytes = xhr.responseText;
cc.log; //print the Chinese character result from XMLHttpRequest.cpp when sent “亂碼測試”
var response = xhr.responseText.substring* “…”;
cc.log(“header:”xhr.getResponseHeader);
var responseLabel = cc.LabelTTF.create: " response, “Thonburi”, 16);
that.addChild(responseLabel, 1);
responseLabel.setPosition(cc.p(350, winSize.height - 220));
statusPostLabel.setString(“Status: Got POST response!” + httpStatus);
}
sys.garbageCollect();
this.scheduleOnce(function(){
cc.log(“POST: GC in next frame…”);
sys.garbageCollect();
});
},
still the unreadable character appeared.
Refer to javascript/TestJavaScript sample from cocos2dx samples folder that I try to test is.
I used XMLHttpRequest to POST something and want to get return of Chinese character with JSON format,
but I got unreadable character that should be Chinese UTF-8 character.
>
I has already set
[…]
in server-side and ensure response Http-Header Content-type is UTF-8 format.
>
My Code is on below:
[…]
>
>
I print string in MinXmlHttpRequest::handle_requestResponse(cocos2d::extension::CCHttpClient sender, cocos2d::extension::CCHttpResponseresponse) , the response result is correct(utf-8), but when transmit to Javascript-side, responseText are unreadable character.
>
Environment: Xcode4.6.3,Cocos2dx2.1.4,Mac Lion
>
Thanks!