Jsonp request with Swedish character gives null response - ajax

I'm using jsonp to request data from a web server to my application (built in sencha). The request has a dynamic parameter called 'sokt'. Sometimes the parameter has a swedish character (å, ä, ö) and sometimes it doesn't.
As long as there's no swedish charachter the server returns the expected result: for example:
http://mywebsite.se/jsonnew.php?sokt=test&_dc=1370095960312&page=1&start=0&limit=25&callback=Ext.data.JsonP.callback31
But if there's a swedish character in the request, the server returns nothing. Example:
http://mywebsite.se/jsonnew.php?sokt=enastående&_dc=1370096101366&page=1&start=0&limit=25&callback=Ext.data.JsonP.callback39
But then if i manually url encode the request enastående to enast%E5ende I get the expected result.
So, to summarize, the problem seems to be that the request from jsonp doesn't url encode the query string, it sends the unicode string which is not accepted by the php script that formats the response (which has a utf_8-header). If this really is the reason this is not working, which I'm not sure of, how would I solve this?
EDIT with code:
This is my request:
Ext.getStore('storen').setProxy({ type: 'jsonp', url: 'http://mywebsite.se/synonymer/jsonnew.php?sokt=' + param}).load()
And this is php script:
<?php
header('Content-Type: text/javascript; charset=utf8');
include("config.php");
$dbh = new PDO(DB_DSN, DB_USERNAME, DB_PASSWORD);
$sokt = $_GET['sokt'];
$stmt = $dbh->prepare("SELECT * FROM table WHERE w1 = :sokt");
$stmt->bindParam(':sokt', $sokt);
$stmt->execute();
$output = array();
while ( $row = $stmt->fetch() ) {
$output[] = array("key" => utf8_encode($row['w2']));
}
$callback = $_REQUEST['callback'];
// Create the output object.
//start output
if ($callback) {
echo $callback . '(' . json_encode($output) . ');';
} else {
echo json_encode($output);
}
?>

You are attaching the value of sokt in the url of the proxy, hence the proxy want change anything. So you have to care about this yourself. There is a native method for that encodeURIComponent()
Ext.getStore('storen').setProxy({ type: 'jsonp', url: 'http://mywebsite.se/synonymer/jsonnew.php?sokt=' + encodeURIComponent(param)}).load()
As I can see someone other already answered this but deleted his answer cause you told him this want work, so here are some additional infomation:
Now your request should be send encoded like this
http://mywebsite.se/jsonnew.php?sokt=enast%C3%A5ende&_dc=1370096101366&page=1&start=0&limit=25&callback=Ext.data.JsonP.callback39
Verify that by using your browsers developer tools. If that is so the frontend has done anything right and you will have to check the backend.
There you have to check if the param is URLdecoded and if not decode it
$sokt = urldecode($_GET['sokt']);
and you have to ensure that your database is really storing all the correct way. So use some tool like PHPMyAdmin (MySQL), ManagementStudio (MS SQL) or any query tool to see if your database behaves correct and the that the Data is stored in the correct format.
Checking all this should lead you to the error

When dealing with utf-8, first make sure everything is configured for utf-8
HTML
PHP, file store (file encoding)
Web Server
Database Columns (Collation)
Database Connection (Client and Server)
If done so, you're almost close to entirely forget about troubles with äöüß (No swedish chars here ;-) ).
And obviously you don't need any utf8_en|decode functions which do not work properly anyway, see the comments on php.net to these functions.
I've recently switched from ISO-8859-1 to utf-8 and it was a hell of work, but afterwards any "char conversion" was necessary any longer at all.
So: leaveing those äöü in an URL unencoded may still lead to problems. I don't kno Ext but assume that their doing a proper UTF-8 encoding of the extraParams as json requires that!
Have you tried
Ext.getStore('storen').setProxy({
type: 'jsonp'
,url: 'http://mywebsite.se/synonymer/jsonnew.php'
,extraParams: {
sokt: encodeURIComponent(params)
}
}).load();

Try using the extraParams option of the proxy instead of hardcoding it in the URL:
Ext.getStore('storen').setProxy({
type: 'jsonp'
,url: 'http://mywebsite.se/synonymer/jsonnew.php'
,extraParams: {
sokt: param
}
}).load();
Or, even simple, the params option of the load() method:
Ext.getStore('storen').load({params: {sokt: param});

Try decoding your param on the server side:
$sokt = urldecode($_GET['sokt']);
Try forcing your database connection to UTF8 before executing your query:
$dbh->prepare("SET NAMES 'utf8'")->execute();
Maybe one of these or the combination of both will work.

Related

Can't send '+' character over ajax

I have a script that makes an ajax request passing 3 different data : eventName / ticketsToWin / totalWinners.
My whole process was working perfectly until I had this case : "SCi+Tec" as the eventName variable. Here is what looks like the data of the request just before sending :
name=Sci+Tec&ticketsToWin=1&totalWinners=2
But then, on the PHP side, if I dump the _GET array, I have this :
array(4) {
["name"]=> string(7) "Sci Tec"
["ticketsToWin"]=> string(1) "1"
["totalWinners"]=> string(1) "2"
["_"]=> string(13) "1372359516001"
}
The '+' character is missing in the name, which breaks everything that comes after. Any idea why ?!
Thans!
encode your string:
name=Sci%2BTec&ticketsToWin=1&totalWinners=2
Or easier:
var str = 'name=Sci+Tec&ticketsToWin=1&totalWinners=2';
var encoded = encodeURIComponent(str);
see the docs or this Question
I'm pretty sure that the plus sign in URLs is used instead of a space, like in a google search, you give the following query:
"How to send an email",
It will show in the URL as:
"How+to+send+an+email".
Try POSTing it.
In Urls, spaces in query strings are automatically replaced by plus signs. So when the server gets Sci+Tec, it thinks there is supposed to be a space there. You will need to escape it with its url encoding: %2B.
More on Url encoding: http://www.w3schools.com/tags/ref_urlencode.asp
you could either use a java url encode or a javascript encoder
URLEncoder.encode(yourString)

Passing parameters through URL in CodeIgniter

I'm having a problem regarding passing parameter from url. The function I use sometimes give the correct result and sometimes not.
here is my controller functions:
public function link_gen(){
$text = "i have lost my password please help me";
$encrypted_text = $this->encrypt->encode($text);
$encrypted_url = urlencode($encrypted_text);
echo $encrypted_url. br();
echo br(). $this->retrive(urldecode($encrypted_url));
echo anchor('encryption/ret_back?username='.$encrypted_url, 'click me');
// echo anchor('encryption/ret_back/'.$encrypted_url, 'click me');
}
public function ret_back(){
// echo br(). $this->retrive(urldecode($str));
$user = $this->input->get('username');
echo br(). $this->retrive(urldecode($user));
echo $user. br();
echo "hellooooo". br();
}
For the sake of testing I'm encrypting a text then encode it in URL using urlencode(), then I pass this string to another function using URL, and then decode in the string in URL using urldecode() and then decode the text. But when I run this, sometimes the decoded text appears and sometimes it does not.
Can anybody tell me the reason and solution for this?
I want to use this in a 'forgot password' module.
It depends on how you create your links and how do you retrieve the data from url.
Anyway these are simple examples to get url data:
get from $_GET[] (http://site.com/?q=hello+world)
in this case you get the param q with echo $this->input->get('q',true);
get data from uri segments (http://site.com/id/230)
in this case you get id value via echo $this->uri->segment(2);
Then, if data in url exists it is not possible that the system didn't get that, so be sure data is in your url when opening url.

Passing a filepath over url

I need to pass this filepath over via route to my actionmethod:
<p>#car.Name</p>
so for example #car.ContainerPath is a string of "34_Creating%20Cars%20Forms/Exercise%20Cars/Audi%202010%20Parts%20Reference.pdf"
I need to escape this somehow I think? I would prefer not to send this over url but with a hyperlink I don't see a way not to.
UPDATE:
For additional info, here's the actionmethod it's going to:
public string GetFileZipDownloadUrl(CarViewModel model, string fileContainerPath)
{
string downloadUrl = string.Empty;
downloadUrl = GetFileZipDownloadUrl(model.CarId,fileContainerPath, model.UserId);
return downloadUrl;
}
so I'm sending over for that fileContainerPath paths like this in the url for that #car.ContainerPath param:
"55_Creating Cars Forms/Exercise Cars/Audi Parts Reference.pdf"
so the route url before it's requested looks like this when formed in that hyperlink:
http://Cars/55/55_Creating Cars Forms/Exercise Cars/Audi Parts Reference.pdf/20/Url
My action method just needs to use that path to go get a reference to a file under the hood.
If you want to just get rid of %20 in the url use encoding/decoding like in #Xander's answer. However if any of your data is very dynamic and can have weird characters you should consider adding a Safe() and Unsafe() methods that will strip out all the "Dangerous" characters for url, and then turn it back to original value.
Raw Url:
HttpUtility.UrlEncode(rawurl);
Decode encoded url:
HttpUtility.UrlDecode(encodedurl);
http://msdn.microsoft.com/en-us/library/system.web.httputility.urlencode.aspx
http://msdn.microsoft.com/en-us/library/system.web.httputility.urldecode.aspx

How do I call an SSJS method with parameters from javascript

I have a url containing a hash e.g http://www.acme.com/home.xsp#key=1234
When the url above loads in the browser I need to call a serverside javascript based on the value in the hash.
I have found a few ways of retriving the hash client side like this
var key = getHashUrlVars()["key"];
so I have the key available in my client side script in the onclientload event.
So in the same onClientLoad event I now need to call my server side javascript method so I have tried the following
'#{javascript:doStuff(key)}'
'#{javascript:doStuff(' + key + ')}'
..and a few other ways. but I can't get it to work.
maybe there is an XSP command I can use instead?
any ideas how to solve this?
You could do a XSP.partialRefreshPost in CSJS and use parameters to send your data to the server:
var p = { "key": getHashUrlVars()["key"] }
XSP.partialRefreshPost( '#{id:_element_to_refresh_}', {params: p} );
To access the parameters in SSJS just try this:
doStuff( param.key )
You could use an empty div-element as a target execute the SSJS code. Or you can use the executeOnServer - method: http://xpages.info/XPagesHome.nsf/Entry.xsp?documentId=88065536729EA065852578CB0066ADEC
Hope this helps
Sven

passing a large string through url in codeigniter

how do i pass a large string as a variable in codeigniter? i am trying show the user an article, if the article has more than 800 characters and less than 3044 characters i am showing it in a jquery pop up window, and if the article is more than 3044 charcters i want to pass the article body and title through the url to a controller function.
here is what i have tried:
<?php
if(strlen($home_content[1]['content'])>800 && strlen($home_content[1]['content'])<3044)
{
$substr=substr($home_content[1]['content'],0,786);
echo $substr.'<p id="button"><i>read more...</i></p>';
}
else if(strlen($home_content[1]['content'])<800)
{
echo $home_content[1]['content'];
}
else
{
$substr=substr($home_content[1]['content'],0,786);
echo $substr.'<br/>';
echo anchor('site/read_article/'.$home_content[1]['title'].$home_content[1]['content'],'<i>read more...</i>');
}
?>
and this is the url after passing the data:
http://192.168.1.111/my_project/site/read_article/title%20mid%20left%3Cp%3Etesttesthave%20already%20arrivesafOnly%20True%20Light,%20reflected%20in%20a%20True%20Mirror,%20can%20come%20to%20lBut%20we%20have%20already%20arrivesafOnly%20True%20Light,%20reflected%20in%20a%20True%20Mirror,%20can%20come%20to%20life.ife.testtesthave%20already%20arrivesafOnly%20True%20Light,%20reflected%20in%20a%20True%20Mirror,%20can%20come%20to%20lBut%20we%20have%20already%20arrivesafOnly%20True%20Light,%20reflected%20in%20a%20True%20Mirror,%20can%20come%20to%20life.ife.testtesthave%20already%20arrivesafOnly%20True%20Light,%20reflected%20in%20a%20True%20Mirror,%20can%20come%20to%20lBut%20we%20have%20already%20arrivesafOnly%20True%20Light,%20reflected%20in%20a%20True%20Mirror,%20can%20come%20to%20life.ife.testtesthave%20already%20arrivesafOnly%20True%20Light,%20reflected%20in%20a%20True%20Mirror,%20can%20come%20to%20lBut%20we%20have%20already%20arrivesafOnly%20True%20Light,%20reflected%20in%20a%20True%20Mirror,%20can%20come%20to%20lifesafOnly%20True%20Light,%20reflected%20in%20a%20True%20Mirror,%20can%20come%20to%20life.ife.testtesthave%20already%20arrivesafOnly%20True%20Light,%20reflected%20in%20a%20True%20Mirror,%20can%20come%20to%20lBut%20we%20have%20already%20arrivesafOnly%20True%20Light,%20reflected%20in%20a%20True%20Mirror,%20can%20come%20to%20lifesafOnly%20True%20Light,%20reflected%20in%20a%20True%20Mirror,%20can%20come%20to%20life.ife.testtesthave%20already%20arrivesafOnly%20True%20Light,%20reflected%20in%20a%20True%20Mirror,%20can%20come%20to%20lBut%20we%20have%20already%20arrivesafOnly%20True%20Light,%20reflected%20in%20a%20True%20Mirror,%20can%20come%20to%20life.ife.%20True%20Mirror,%20can%20come%20to%20life.ife.%20True%20Mirror,%20can%20come%20to%20life.ife.%3C/p%3E%3Cp%3E%C2%A0%3C/p%3E%3Cp%3Etesttesthave%20already%20arrivesafOnly%20True%20Light,%20reflected%20in%20a%20True%20Mirror,%20can%20come%20to%20lBut%20we%20have%20already%20arrivesafOnly%20True%20Light,%20reflected%20in%20a%20True%20Mirror,%20can%20come%20to%20life.ife.testtesthave%20already%20arrivesafOnly%20True%20Light,%20reflected%20in%20a%20True%20Mirror,%20can%20come%20to%20lBut%20we%20have%20already%20arrivesafOnly%20True%20Light,%20reflected%20in%20a%20True%20Mirror,%20can%20come%20to%20life.ife.testtesthave%20already%20arrivesafOnly%20True%20Light,%20reflected%20in%20a%20True%20Mirror,%20can%20come%20to%20lBut%20we%20have%20already%20arrivesafOnly%20True%20Light,%20reflected%20in%20a%20True%20Mirror,%20can%20come%20to%20life.ife.testtesthave%20already%20arrivesafOnly%20True%20Light,%20reflected%20in%20a%20True%20Mirror,%20can%20come%20to%20lBut%20we%20have%20already%20arrivesafOnly%20True%20Light,%20reflected%20in%20a%20True%20Mirror,%20can%20come%20to%20lifesafOnly%20True%20Light,%20reflected%20in%20a%20True%20Mirror,%20can%20come%20to%20life.ife.testtesthave%20already%20arrivesafOnly%20True%20Light,%20reflected%20in%20a%20True%20Mirror,%20can%20come%20to%20lBut%20we%20have%20already%20arrivesafOnly%20True%20Light,%20reflected%20in%20a%20True%20Mirror,%20can%20come%20to%20lifesafOnly%20True%20Light,%20reflected%20in%20a%20True%20Mirror,%20can%20come%20to%20life.ife.testtesthave%20already%20arrivesafOnly%20True%20Light,%20reflected%20in%20a%20True%20Mirror,%20can%20come%20to%20lBut%20we%20have%20already%20arrivesafOnly%20True%20Light,%20reflected%20in%20a%20True%20Mirror,%20can%20come%20to%20life.ife.%20True%20Mirror,%20can%20come%20to%20life.ife.%20True%20Mirror,%20can%20come%20to%20life.ife.%3C/p%3E%3Cp%3Etesttesthave%20already%20arrivesafOnly%20True%20Light,%20reflected%20in%20a%20True%20Mirror,%20can%20come%20to%20lBut%20we%20have%20already%20arrivesafOnly%20True%20Light,%20reflected%20in%20a%20True%20Mirror,%20can%20come%20to%20life.ife.testtesthave%20already%20arrivesafOnly%20True%20Light,%20reflected%20in%20a%20True%20Mirror,%20can%20come%20to%20lBut%20we%20have%20already%20arrivesafOnly%20True%20Light,%20reflected%20in%20a%20True%20Mirror,%20can%20come%20to%20life.ife.testtesthave%20already%20arrivesafOnly%20True%20Light,%3C/p%3E.html
and i get this error message:
An Error Was Encountered
The URI you submitted has disallowed characters.
how do i do it correctly? the url looks very messy, how do pass the string and still have a clean url? please help me with it.
Why not pass the article ID instead? You could then access the article through the controller function, count the characters and decide the method of display.
Alternatively, you could use CI's Session Flashdata to pass the article title/body to the next controller and access it that way.
The URI is failing as security is set up to deny specific characters being passed in the URL. This is for your protection, but, although not recommended, could be disabled in the config files if required.

Resources