JSON messes up my diacritics - ajax

I'm sending a string using jQuery AJAX POST and JSON:
$.ajax({
type: "POST",
dataType: "JSON",
url: "someUrl.asp?param1=someParam1",
contentType: "charset=utf-8",
data: JSON.stringify({
some_code: $( "#some_code" ).length > 0 ? $("#some_code").val() : ''
})
})
Serverside is VBScript/ASP.
some_code is a textbox with following text: someValue čšžćđ that needs to be saved just like that.
When scanning network traffic (IE9) I see this:
some_code=someValue Äšžćđ
When looking in the database (Oracle 12c) I see this:
someValue ?????
Html page encoding is Windows-1250.
NLS_LANG and NLS_CHARACTERSET are Slovenian.
I've tried out advice from at least a dozen different links, but to no avail, so I'm turning to you guys and girls. Thank you!

Right from the comments think I understand the issue;
The JSON has to be sent as contentType: "charset=utf-8" so the page someUrl.asp will also need to process in UTF-8 to do this follow the below steps.
Based on your comment have made some changes to the below code.
First re-save the someUrl.asp file using UTF-8 encoding not ASCII.
Set the first line in someUrl.asp to;
<%#Language="VBScript" CodePage = 65001 %>
Then add the following lines;
<%
Response.Charset = "Windows-1250"
Response.CodePage = 1250
%>
Note: When making changes always remember to save the file with UTF-8 encoding.

Related

Corrupted PDF When Saving to Database over 50kB

I have a webpage where the user can upload a PDF file and send it using AJAX to my Flask application. Here is the ajax code:
var formData = new FormData();
formData.append('attachment', document.getElementById("attachment").files[0]);
$.ajax({
type: 'POST',
url: 'process_award_storage',
contentType: false,
processData: false,
data: formData
})
I do not think that this has any problems because I can print out the content and title of file in the python code. Then my flask model is defined as such, using LargeBinary for the PDF attachment:
class AwardStore(db.Model):
__tablename__ = 'awards_store'
id = db.Column(db.Integer, primary_key=True)
...
file = db.Column(db.LargeBinary, nullable=True) #I am sure this is the right file type for PDF saving
Lastly, here is how the AJAX file is saved to the database:
award = AwardStore(name=name, file=request.files['attachment'].read())
db.session.add(award)
db.session.commit()
I can see using my MySQL Workbench that it is saved. However, when I try to download the BLOB from there to the Desktop and open it, it says "Failed to load PDF document". The same happens when I use flask's send_file. It seems like I did everything as I saw online, but something is wrong.
Maybe these warnings are related?:
C:\Users\msolonko\Desktop\NICKFI~1\CACHTM~1\APP_DE~1\virt\lib\site-packages\pymysql\cursors.py:170: Warning: (1300, "Invalid utf8 character string: 'C4E5F2'")
result = self._query(query)
C:\Users\msolonko\Desktop\NICKFI~1\CACHTM~1\APP_DE~1\virt\lib\site-packages\pymysql\cursors.py:170: Warning: (1265, "Data truncated for column 'file' at row 1")
result = self._query(query)
I tried googling them and did not find anything. I appreciate any assistance.
EDIT:
I noticed that small files are typically uploaded and displayed properly. The issue is with files with sizes > ~50kB. The database I am using the AWS RDS, Is there a setting I can change somewhere to enable greater sizes?
The LargeBinary accepts a length field also.
which converts to mysql's BLOB whose default length is 65535 bytes (64kb).
Try increasing the length and set it to
a MEDIUMBLOB for 16777215 bytes (16 MB)
a LONGBLOB for 4294967295 bytes (4 GB).
Hope this will help

requests.get() not retrieving correct url in python 2.7

I'm trying to access url and then parse it's contents based on tags.
My code:
page = requests.get('https://support.apple.com/downloads/')
self.tree = html.fromstring(page.content)
names = self.tree.xpath("//span[#class='truncate_name']//text()")
Problem: variable page is containing data that of url 'https://support.apple.com/'
I'm new to python 2.7. The whole encoding issues in file. I'm using unicode-escape as my default encoding. Encoding on resource at https://support.apple.com/downloads/ is utf-8 whereas encoding of resource at https://support.apple.com/ is variable. Is this has something to do with the problem? Please suggest solution for this.
It has nothing to do with encoding , what you are looking for is dynamically created so not in the source you get back. A series of ajax calls populates the data. To get the product names etc.. from the carousel where you see the span.truncate_name in your browser:
params = {"page": "products",
"locale": "en_US",
"doctype": "DOWNLOADS",
}
js = requests.get("https://km.support.apple.com/kb/index", params=params).content
Normally we could call .json() on the response object but in this case we need to use "unicode_escape" then call loads:
from json import loads, dumps
js2 = loads(js.decode("unicode_escape"))
print(js2)
Which gives you a huge dict of data like:
{u'products': [{u'name': u'Servers and Enterprise', u'urlpath': u'serversandenterprise', u'order': u'', u'products': .............
You can see the request in chrome tools:
We leave off callback:ACDow‌​nloadSearch.customCa‌​llBack as we want to get back valid json.

MSXML2.ServerXMLHTTP and national characters

This question is related to this one: Character encoding Microsoft.XmlHttp in Vbscript, but differs in one thing, the national characters are in the domain name, not only arguments.
The task is: download a page from the given URL.
I already solved problem of passing UTF8 string into VBScript by reading it from UTF8 encoded file through ADO.
But now when I try opening it MSXML2.ServerXMLHTTP returns error: The URL is invalid.
Here is VBScript code:
Set objStream = CreateObject("ADODB.Stream")
objStream.CharSet = "utf-8"
objStream.Open
objStream.LoadFromFile("fileWithURL.txt")
url = objStream.ReadText()
objStream.Close
Set XMLHttpReq = CreateObject("MSXML2.ServerXMLHTTP")
XMLHttpReq.Open "GET", url, False
XMLHttpReq.send
WEBPAGE = XMLHttpReq.responseText
If you put something like hxxp://россия.рф/main/page5.html into the UTF8 encoded fileWithURL.txt the script will raise an error while working ok with hxxp://google.com.
The workaround is to use ascii representation of the domain name - but I yet haven't found PunnyCode encoder for vbscript (apart from Chillkat which is an overkill for my task).
Will appreciate your help on the main problem or workaround.
I've made an amazing journey in to depth of my hard drive and found a code writen by / for Jesper Høy. This was the source code of SimpleDNS Plus' IDN Conversion Tool at that time.
Archive.org page snapshot: http://www.simpledns.com/idn-convert.asp
Archive.org file snapshot: idn-convert-asp.zip
You can also copy the whole code from this gist.
Create a function to convert URLs.
Function DummyPuny(ByVal url)
Dim rSegments : rSegments = Split(url, "/")
If UBound(rSegments) > 1 Then
rSegments(2) = DomainPunyEncode(rSegments(2))
End If
DummyPuny = Join(rSegments, "/")
End Function
Then convert your url before making the request.
XMLHttpReq.Open "GET", DummyPuny(url), False

How to get Cyrillic responsetext

Dim http As WinHttpRequest
Set http = New WinHttpRequest
http.open "POST", "test.php", False
http.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
http.send "txtmbx=test"
msgbox(http.responsetext)
http.responsetext is in Cyrillic and I'm unable to read text. How can I retrieve Cyrillic?
The WinHTTPRequest does not support an encoding method, and while it doesn't explicitly say, I expect it uses CP_ACP, the system default codepage when converting from the received byte data to a string.
You can use the ResponseBody method to get the data as a byte array and use StrConv to convert to a string as you wish.

ajax(search suggest) funny character problem

ajax(search suggest), if input funny character(like Ô) and submit it, "?" is displayed in *****.asp. ( response.write (request.form("str")))
i am using
xmlhttp.open("post", "*****.asp", true);
xmlhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded; charset=UTF-8');
xmlhttp.send("str="+escape($("str").value));
and there is <%#CODEPAGE=65001%> in *****.asp file
How can i get the correct word--- "Ô" in *****.asp
escape() is terrible, avoid it at all costs. Try this:
xmlhttp.send("str="+encodeURIComponent($("str").value));
This would encode Ô into %C3%94 - assuming the page decoding it supports utf8 you should be fine.

Resources