send japanese character through XMLHttpRequest - javascript

I have to send the japanese name trough XMLHttpRequest. but it displays encoding problem... my tpl page is in utf-8 charset.
here is my code.
function getFormData(dno,rno) {
var name = document.getElementById("f_nickname").value;
var digNo = dno;
var resNo = rno;
var strVal = digNo + "-" + resNo;
stp.push(strVal);
var xhr = new XMLHttpRequest();
if (!xhr) return false;
var url = 'ajax.php' + '?prc=' + 'diagnoses' + '&name=' + name + '&diagres=' + stp;
xhr.open('POST', url, true);
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.send(stp);
return true;
}
i have passing url like this:
http://crp.com/ajax.php?prc=diagnoses&name=大阪&diagres=0-0,1-3,2-2,3-3,4-3,5-2
but it displays in Ajax.php like
http://crp.com/ajax.php?prc=diagnoses&name=ƒsƒU&diagres=0-0,1-3,2-2,3-3,4-3,5-2
tried in many ways... How to solve?
thanks in advance...

Use encodeURIComponent for such parameters in url.
var url = 'ajax.php' + '?prc=' + 'diagnoses' + '&name=' + encodeURIComponent(name) + '&diagres=' + stp;
It will result in url like:
http://crp.com/ajax.php?prc=diagnoses&name=%E5%A4%A7%E9%98%AA&diagres=0-0,1-3,2-2,3-3,4-3,5-2
And webserver will handle it properly.

Try adding:
xmlHttp.setRequestHeader('Content-type', 'text/xml;charset=utf-8');
Use:
encodeURIComponent(name) instead of just name

Related

Import WSDL file into Javascript

I want to import a WSDL in XML file to my JavaScript project, which will allow my to use web client to search info.
Is there any special way to import it into my project, or do I need a package, or need to convert it into JS? I know there is a soap package in NPM, but I hope it's not the only way to do that. I've also read tutorials on W3 and still not quite sure how to manage that.
I've got something like this:
function soap() {
var xmlhttp = new XMLHttpRequest();
xmlhttp.open('POST', '127.0.0.1/start.html', true);
// build SOAP request
var sr =
'<soapenv:Envelope ' +
'xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" ' +
'xmlns:aut="http://demo.krd.pl/Authorization" xmlns:dto="http://demo.krd.pl/Chase3.1/Dto"> ' +
'<soapenv:Header> ' +
'<aut:Authorization> '+
'<aut:AuthorizationType>LoginAndPassword</aut:AuthorizationType> ' +
'<aut:Login>login</aut:Login> ' +
'<aut:Password>password</aut:Password> ' +
'</aut:Authorization> ' +
'</soapenv:Header> ' +
'<soapenv:Body> ' +
'<dto:SearchNonConsumerRequest> ' +
'<dto:Number>24041803749</dto:Number> ' +
'<dto:NumberType>TaxId</dto:NumberType> ' +
'</dto:SearchNonConsumerRequest> ' +
'</soapenv:Body> ' +
'</soapenv:Envelope> ';
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4) {
if (xmlhttp.status == 200) {
alert('done. use firebug/console to see network response');
}
}
}
// Send the POST request
xmlhttp.setRequestHeader('Content-Type', 'text/xml');
xmlhttp.send(sr);
// send request
// ...
}
Also, where the response will be saved or will it just be posted in console?

Send informations with AJAX, error "mycustompage.htm?aspxerrorpath="

I use Javascript and AJAX to send some informations in a .NET MVC project. The code below works on my local machine but not on my server.
var str = "/Nouvelle_Fiche" + "/" + id_fiche + "/" + id_process + "/" + type_process + "/" + id_impact + "/" + id_auteur + "/" + id_situation + "/" + questionnaires;
xmlhttp.open("POST", str, true);
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlhttp.send();
I have an error "404 Not Found" with the line "xmlhttp.send();" because it add to my link "mycustompage.htm?aspxerrorpath=" so i have :
"mycustompage.htm?aspxerrorpath=/ Nouvelle_Fiche/17/13766/I/43/1162/1/2_"
Can you help me please ? Thank you !
The main points MVC controller doesn't understand what you are trying to call.
I believe your route config looks like this:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}/{process}/{typeprocess}/{impact}/{auteur}/{situation}/{questionnaires}",
defaults: new { controller = "Home", action = "Nouvelle_Fiche", id = UrlParameter.Optional,
process = UrlParameter.Optional, typeprocess = UrlParameter.Optional,
impact = UrlParameter.Optional, auteur = UrlParameter.Optional,
situation = UrlParameter.Optional, questionnaires = UrlParameter.Optional}
);
Assuming that Controller name is: "Home" and action is "Nouvelle_Fiche" which take 7 parameters
then your request should be like this:
xmlhttp.open("POST", #Url.Action("Home", "Nouvelle_Fiche", new { id = id_fiche,
process = id_process , typeprocess = type_process,
impact = id_impact, auteur = id_auteur,
situation = id_situation, questionnaires = questionnaires }), true);
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlhttp.send();

JavaScript: xmlhttprequest request and server answer

I'm starting off with js.
I want to fetch data from a server with xml. I wonder how to send a request as, and get an anser in xml through javascript functions.
It says I need a POST-Request and send an xml in the form:
<?xml version="1.0" encoding="UTF-8"?>
<ft>
<request clientId="123" apiName="api_search_location_stops_nearby" apiVersion="2.0">
<client clientId="123"/>
<requestType>api_search_location_stops_nearby</requestType>
<outputCoords>WGS84</outputCoords>
<fromCoordName>WGS84</fromCoordName>
<fromType>coords</fromType>
<fromWgs84Lat>48.22</fromWgs84Lat>
<fromWgs84Lon>16.39</fromWgs84Lon>
</request>
</ft>
To then get an xml answer. It has 2 or 3 nodes in it, which I'm interested in. From there on, it'll be no big deal.
It is all about a strange API from the vienna public transport company:
http://akirk.github.io/Wiener-Linien-API/
I basically want to get (open)data from them.
Here:
https://techscreen.tuwien.ac.at/node/794
I found a solution for php..
My try:
// Bare bones XML writer - no attributes
function xmlElement(name,content){
var xml
if (!content){
xml = '<' + name + '>' + '</' + name + '>'
}
else {
xml = '<'+ name + '>' + content + '</' + name + '>'
}
return xml
}
function sendRequest()
{
var xmlReq
xmlReq = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
xmlReq = xmlReq + "<ft>";
xmlReq = xmlReq + "<request clientId=\"123\" apiName=\"api_get_monitor\" apiVersion=\"2.0\">";
xmlReq = xmlReq + "<client clientId=\"123\"/>";
xmlReq = xmlReq + xmlElement("requestType", "api_get_monitor");
xmlReq = xmlReq + xmlElement("monitor",
xmlElement("outputCoords", "WGS84") +
xmlElement("type","stop") +
xmlElement("name","60201040") +
xmlElement("year","2013") +
xmlElement("month","10") +
xmlElement("day","3") +
xmlElement("hour","8") +
xmlElement("minute","0") +
xmlElement("line") +
xmlElement("sourceFrom","stoplist") );
xmlReq = xmlReq + "</request>" + "</ft>";
text1.text = xmlReq;
var xhr = new XMLHttpRequest();
xhr.onload = handleRequest;
xhr.open("POST", "http://webservice.qando.at/2.0/webservice.ft"); // POST or GET
xhr.send(xmlReq);
// xhr.responseXML // this is allways null
}
function handleRequest(answer)
{
console.log(answer.responseType);
console.log(answer.responseXML);
}
The core points of my question: On my code, should there be GET or POST? Is the request built up to fit the style above (or do I need linebreaks or convert to a DOM xml thing)? How does the recieving thing work. Am I doing this right? Should the variable answer then contain the xml with the answer?
This code is somehow not working. I printed the xml-string to the console and it looks just like above (without linebreaks). But the handleRequest function doesn't print anything (it is not called at all).
Thanks in advance!
Looks like it was good allready.
I got the wrong clientID. That was basically it. Then there was just a small modification to make:
xhr.onreadystatechange = function(){ page.handleRequest(xhr); }
..to send the xmlHttpRequest over to the function. This worked for me.

URL being encoded wrong

I'm getting confused passing the http:// in a string to the url as it is being stripped to
http%3A%2F%2F
I tried.. using the encodeURIComponent(http://)
but that didn't work either..
I'm trying to pass the url into here:
https://www.facebook.com/sharer/sharer.php?url=
Here is my code that isn't working:
$(document).on('click', '.fb', function(e) {
var findfb = $('.fb').parent().parent().parent().find(".zoomy").attr("src");
var facebook_url = 'http://www.facebook.com/sharer.php?url=http://www.site.com/folder1/'+encodeURIComponent(findfb)+
'&title='+encodeURIComponent('title of page');
window.open(facebook_url);
});
Just do it as simple as:
var facebook_url = 'http://www.facebook.com/sharer.php?url=' + encodeURIComponent('http://www.site.com/folder1/' + findfb +'&title=' + 'title of page');
var facebook_url = 'http://www.facebook.com/sharer.php?url=' + encodeURIComponent('http://www.site.com/folder1/' + findfb) + '&title=' + encodeURIComponent('title of page');

xhr response: connection closed

I wrote a Flickr search engine that makes a call to either a public feed or a FlickrApi depending on a selected drop down box.
examples of the JSONP function calls that are returned:
a) jsonFlickrApi({"photos":{"page":1, "pages":201, "perpage":100, "total":"20042", "photo":[{"id":"5101738723"...
b) jsonFlickrFeed({ "title": "Recent Uploads tagged red","link": "http://www.flickr.com/photos/tags/red/","description": "", ....
the strange thing is that in my local install (xampp) both work fine and i get images back BUT when i host the exact same code on the above domain then the jsonFlickrApi doesn't work. What i notice (by looking at Firebug) is that for the jsonFlickrApi the response Header says Connection close
Also, Firebug doesn't show me a Response tab when i submit a request to the jsonFlickrApi
here is the code:
function makeCall(uri)
{
xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = callback;
xmlhttp.open("GET", "jsonget.php?url="+uri, true);
xmlhttp.send();
}
function jsonFlickrApi(response)
{
var data= response.photos.photo ;
var output = "";
output += "<img src=http://farm" + data[4].farm + ".static.flickr.com/" + data[1].server + "/" + data[4].id + "_" + data[4].secret + ".jpg>";
document.getElementById("cell-0").innerHTML = output ;
}
//Public Feed
function jsonFlickrFeed(response)
{
var data= response.items[0].media.m ;
alert(data);
var output = "";
output += "<img src=" + data+ ">";
document.getElementById("cell-0").innerHTML = output ;
}
function callback()
{
//console.log("Ready State: " + xmlhttp.readyState + "\nStatus" + xmlhttp.status);
if(xmlhttp.readyState==4 && xmlhttp.status==200)
{
var jsonResponse = xmlhttp.responseText;
jsonResponse = eval(jsonResponse);
}
}
examples of calls:
a)
http://flickr.com/services/rest/?method=flickr.photos.search&api_key=75564008a468bf8a284dc94bbd176dd8&tags=red&content_type=1&is_getty=true&text=red&format=json&timestamp=1339189838017
b)
http://api.flickr.com/services/feeds/photos_public.gne?tags=red&format=json&timestamp=1339190039407
Question: why does my connection close? why is it working on localhost and not on the actual domain?
Looking at the HTTP response headers of
http://flickr.com/services/rest/?method=flickr.photos.search&api_key=75564008a468bf8a284dc94bbd176dd8&tags=red&content_type=1&is_getty=true&text=red&format=json&timestamp=1339189838017
I get a 302 with location
http://www.flickr.com/services/rest/?method=flickr.photos.search&api_key=75564008a468bf8a284dc94bbd176dd8&tags=red&content_type=1&is_getty=true&text=red&format=json&timestamp=1339189838017
So, what flicker wants to tell you is "use www.flicker.com instead of flicker.com!". With this URL I get content.

Categories

Resources