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 在 dev 和 live 中也可以,但在一些使用 Firefox V21.0 的朋友的机器上,它也只能在 dev 中工作。

我在标题中进行了研究。带有工作 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 的响应正文中,有“刷新页面,以获取源代码: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 中也会出现问题。测试的是现场系统。

4

1 回答 1

1

那里的编码员,找到了解决方案。当请求和响应 url 不同时,就会出现神秘的“302 Found”状态。这个简单的观察使我检查了我的 ajax 调用中的 url 选项,瞧。有错误。由于我通过 php 进行硬编码的动态 url 打印“ http://www.xyz.de/controller/action”,cake假设重定向从http://www.xyzhttp://xyz内部由 cake 与它的 .htacces 匹配,其中有一个自己的重定向url 被存储,并触发了 302。在将 url 固定为“/action”或喜欢需要的“/controller/action”之后,现在一切都像魅力一样工作了。

除了您的 ajax-bug-tracking 之外,我建议使用 cakes 丑陋但有效代码交付 js-helper 打印普通 jquery(如果用作 js-library)来查看。通过这一举动,我发现了错误。

问候和快乐的编码:)

于 2013-06-08T20:51:32.823 回答