简体   繁体   中英

PHP parse Ajax-request sent from Internet Explorer

I'm working on a real-time timetable for a scool. Now when i'm testing it i'm getting an Error only in internet explorer . I use ie v.11 and charset UTF-8

When i send the data of the current student to a php-script there are some characters like ä,ö,ü which get parse to a ?. But only in InternetExplorer.

if i sent a URL like this: http://adopraesenz.ipwin.ch/data/students.php?q=getWirth Nalia;3:Kürzel;0;0;4;0;0;0;

and want to receive them in the php like this

$q = $_REQUEST["q"];
echo $q;

i get this output: getWirth Nalia;3:K?rzel;0;0;4;0;0;0;

I'm sending data like this:

function getData(str) {
            var requestURL = "http://adopraesenz.ipwin.ch/data/students.php?q=" +str;
            var request = new XMLHttpRequest();
            request.onreadystatechange = function() {           
                if(this.readyState === 4 && this.status === 200){
                    loadJson(request);
                }
            };
            request.open("GET", requestURL, true);
            request.send();
        }

for more code or information please ask.

In PHP you should always use urlencode and urldecode if you want to send data over a GET request. First encode your parameters, and after receiving decode them again. Most webservers will do the decoding for you, so you only have to call the encode code most of the time. Also check that your form is using the same charset in html and http-header (utf-8 prefered)

Examples from official php documentation

encode

<?php
echo '<a href="mycgi?foo=', urlencode($userinput), '">';

decode (webserver will do this for you most of the time)

<?php
$query = "my=apples&are=green+and+red";

foreach (explode('&', $query) as $chunk) {
    $param = explode("=", $chunk);

    if ($param) {
        printf("Value for parameter \"%s\" is \"%s\"<br/>\n", urldecode($param[0]), urldecode($param[1]));
    }
}

An easier solution would be to use a post request instead of get, this is the prefered way for sending json data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM