On the Django server side, I form a response:
def index(request):
if request.method == 'POST':
...
url = reverse(settlement_create, args=[settlement_id])
return HttpResponse(marshal.dumps({'url': url}), content_type="application/json")
, where url=/settlement/154/create
. jQuery cannot decode the byte string correctly and it turns out {� url� /settlement/154/create0
.
$.ajax({
type : 'POST',
url : url,
data : {'text': city_info},
success : function(response){
$("#city-href").attr('href', response['url']);
},
error : function(response){
console.log(response)
}
});
If you use a regular json library instead of marshal, then everything works fine. Do you have any ideas?
2
Answers
In this code, the Uint8Array constructor is used to create a view of the ArrayBuffer that can be populated with the byte values from the byte string. The TextDecoder API is then used to decode the ArrayBuffer into a string using the UTF-8 encoding.
Note that the TextDecoder API may not be supported in all browsers. If you need to support older browsers, you may need to use a polyfill or alternative library for decoding byte strings.
I guess something is wrong with the encoder. You can just let Django do the work with a
JsonResponse
[Django-doc]: