1

私のAJAXには本当に奇妙な問題があります。Chrome、IE、および Safari を使用すると、次の AJAX 呼び出しが私の開発システムとライブ システムで完全に機能します。

var request = $.ajax({
    url: "http://domain.de<?php echo $this->webroot."Posts/plzResults"; ?>",
    type: "POST", 
    xhrFields: {withCredentials:true}, 
    dataType:"json", 
    async: true,
    data: form,
    success: function (response) {
    $(".plzSearchLoading").fadeOut(200);
    $("#ajaxSearchSCourierDiv").fadeIn(200);//.html(response);
    {
        $('#ajaxSearchSCourierDiv').html('');
        var parsed = JSON.parse(response);
        if (response == '[]') {
            $('#ajaxSearchSCourierDiv').append('<div class="zipInputResults"><table<tr><td>Postleitzahl ungültig.</td></tr></table></div>');
        } else {
            $('#ajaxSearchSCourierDiv').append('<div class="zipInputResults"><table>');                                                             
        for (prop in response) {
            var zip = parsed[prop].Zipcode;
            var city = parsed[prop].City;
            zip = zip.trim();
            city = city.trim();
            $('#ajaxSearchSCourierDiv .zipInputResults table').append('<tr class="post-hover zipClick" onClick="setValues(\''+ zip +'\', \''+ city +'\')"><td>' + zip + '</td><td>' + city + '</td></tr>');                                                             }
            $('#ajaxSearchSCourierDiv').append('</table></div>');                                                               
        }
    }                                                       
},
error: function (xmlHttpRequest, textStatus, errorThrown) {
if(xmlHttpRequest.readyState == 0 || xmlHttpRequest.status == 0) 
return;  // it's not really an error
else
alert( "Da ging was schief. Bitte probiere es nochmal." );
}
});

対応する Cake コントローラーのコードは次のようになります。

public function plzResults() {
        $this->layout = NULL;
        $this->autoRender = false;
        // Suche nach PLZ und Städten, damit User die anklicken kann bei Inserat erstellen
        if($this->RequestHandler->isAjax()){
            //je nachdem, ob suche-oder biete-feld und ob buchstaben oder zahlen eingegeben werden
            if (isset($this->request->data['ajaxSearchSCourier'])) {
                if (!is_numeric($this->request->data['ajaxSearchSCourier'])) {
                    $city = $this->request->data['ajaxSearchSCourier'];
                } else {
                    $zipcode = $this->request->data['ajaxSearchSCourier'];
                }
            };
            if (isset($this->request->data['ajaxSearchSHelp'])) {
                if (!is_numeric($this->request->data['ajaxSearchSHelp'])) {
                    $city = $this->request->data['ajaxSearchSHelp'];
                } else {
                    $zipcode = $this->request->data['ajaxSearchSHelp'];
                }
            };
            if (isset($this->request->data['ajaxSearchBCourier'])) {
                if (!is_numeric($this->request->data['ajaxSearchBCourier'])) {
                    $city = $this->request->data['ajaxSearchBCourier'];
                } else {
                    $zipcode = $this->request->data['ajaxSearchBCourier'];
                }
            };
            if (isset($this->request->data['ajaxSearchBHelp'])) {
                if (!is_numeric($this->request->data['ajaxSearchBHelp'])) {
                    $city = $this->request->data['ajaxSearchBHelp'];
                } else {
                    $zipcode = $this->request->data['ajaxSearchBHelp'];
                }
            };
            if (isset($this->request->data['Post']['ZIPCODE'])) {
                if (!is_numeric($this->request->data['Post']['ZIPCODE'])) {
                    $city = $this->request->data['Post']['ZIPCODE'];
                } else {
                    $zipcode = $this->request->data['Post']['ZIPCODE'];
                }
            };
            if (isset($this->request->data['Post']['DELIVERYAREA'])) {
                if (!is_numeric($this->request->data['Post']['DELIVERYAREA'])) {
                    $city = $this->request->data['Post']['DELIVERYAREA'];
                } else {
                    $zipcode = $this->request->data['Post']['DELIVERYAREA'];
                }
            };
            if (isset($this->request->data['Post']['CITY']) && $this->request->data['Post']['CITY'] <> '') {
                if (is_numeric($this->request->data['Post']['CITY'])) {
                    $zipcode = $this->request->data['Post']['CITY'];
                } else {
                    $city = $this->request->data['Post']['CITY'];
                }
            };
            //geo-db laden
            $this->loadModel('Geonames');
            if (isset($zipcode)) {
                $cityId = $this->Geonames->find('all', array('conditions' => array('Geonames.postal_code LIKE' => $zipcode . '%')));
            };
            if (isset($city)) {
                $cityId = $this->Geonames->find('all', array('conditions' => array('Geonames.place_name LIKE' => '%' . $city . '%')));
            };
            $zipTemp = Hash::extract($cityId, '{n}.Geonames.postal_code');
            $cityTemp = Hash::extract($cityId, '{n}.Geonames.place_name');
            //im array $zipAndCity Paare bilden: array( int (0) => array('Zipcode' => 'blub', 'City' => 'blub'), int (1) ...)
            $zipAndCity = array();
            foreach($zipTemp as $key => $item) {
                $key = "Zipcode";
                $zipAndCity[] = array($key => $item);
            };
            $index = 0;
            foreach($cityTemp as $key => $item) {
                $key = "City";
                $zipAndCity[$index] += array($key => $item);
                $index += 1;
            };
            //sortieren nach Zipcode und vars setzen
            sort($zipAndCity);
            $zipAndCity = json_encode($zipAndCity);
            echo json_encode($zipAndCity);
        }
    }

しかし、今では本当に奇妙なことです。私のマシンの Firefox V21.0 は、開発とライブでも機能しますが、Firefox V21.0 を使用している友人のマシンでは、開発でのみ機能します。

ヘッダーについて調べてみました。AJAX 呼び出しが機能するヘッダー:

リクエストヘッダー

Accept  application/json, text/javascript, */*; q=0.01

Accept-Encoding gzip, deflate

Accept-Language de-de,de;q=0.8,en-us;q=0.5,en;q=0.3

Cache-Control   no-cache

Connection  keep-alive

Content-Length  22

Content-Type    application/x-www-form-urlencoded; charset=UTF-8

Cookie  __utma=117500061.1300628794.1370282236.1370505293.1370510250.8; __utmz=117500061.1370282236.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __atuvc=53%7C23; CAKEPHP=1923d0295000a4abdbc37ba811bbbad5; __utmb=117500061.2.10.1370510250; __utmc=117500061

Host    domain.de

Pragma  no-cache

Referer http://domain.de/Posts

User-Agent  Mozilla/5.0 (Windows NT 6.1; WOW64; rv:21.0) Gecko/20100101 Firefox/21.0

X-Requested-With    XMLHttpRequest

応答ヘッダー

Cache-Control   max-age=60

Connection  close

Content-Encoding    gzip

Content-Length  140

Content-Type    text/html

Date    Thu, 06 Jun 2013 09:17:43 GMT

Expires Thu, 06 Jun 2013 09:18:43 GMT

Server  Apache

Vary    Accept-Encoding

X-Powered-By    PHP/5.2.17

AJAX 呼び出しに失敗したヘッダー:

リクエストヘッダー

Accept  application/json, text/javascript, */*; q=0.01

Accept-Encoding gzip, deflate

Accept-Language de-de,de;q=0.8,en-us;q=0.5,en;q=0.3

Cache-Control   no-cache

Connection  keep-alive

Content-Length  22

Content-Type    application/x-www-form-urlencoded; charset=UTF-8

Cookie  __utma=117500061.1398540368.1370505971.1370505971.1370509889.2; __utmc=117500061; __utmz=117500061.1370505971.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __atuvc=3%7C23; CAKEPHP=99cb20f648db7b7c89855f43c5c453de; __utmb=117500061.3.10.1370509889

DNT 1

Host    domain.de

Origin  http://www.domain.de

Pragma  no-cache

Referer http://www.domain.de/Posts

User-Agent  Mozilla/5.0 (Windows NT 6.1; WOW64; rv:21.0) Gecko/20100101 Firefox/21.0

さらに、plz-results を書き込む必要がある応答本文には、"Refresh page, to get source code for: http://domain.de/Posts/plzResults "が立っています。

応答ヘッダー

Cache-Control   no-store, no-cache, must-revalidate, post-check=0, pre-check=0

Connection  close

Content-Encoding    gzip

Content-Length  20

Content-Type    text/html; charset=UTF-8

Date    Thu, 06 Jun 2013 09:27:54 GMT

Expires Mon, 26 Jul 1997 05:00:00 GMT

Last-Modified   Thu, 06 Jun 2013 09:27:54 GMT

Server  Apache

Vary    Accept-Encoding

X-Powered-By    PHP/5.2.17

あなたへの私の質問: すべてのブラウザーで機能する ajax 呼び出しを取得するにはどうすればよいですか? ありがとうございました :)


追加の注記 1:

さまざまなコンピューターでさらに調査した後、IEでも問題が発生することがあります。テストしたのは live-system でした。

4

1 に答える 1