简体   繁体   English

无法在Chrome中加载资源+ $ .ajax调用

[英]Failed to load resource + $.ajax call in Chrome

I am having a very strange issue with Chrome and AJAX, I have an autocomplete form that has been working for a while. 我在使用Chrome和AJAX时遇到一个非常奇怪的问题,我有一个自动填写表格,该表格已经使用了一段时间。 I fired up Visual Studio this morning and it doesn't work anymore. 我今天早上启动了Visual Studio,它不再起作用了。 It works fine in production (with Chrome) and works fine locally if I use Firefox or IE, for chrome it doesn't! 如果我使用Firefox或IE,它在生产环境(使用Chrome)中可以正常工作,并且在本地也可以,而对于chrome,则不能!

I get the error: 我得到错误:

Failed to load resource 无法加载资源

in Developer tools, When I expand on the error I get: 在开发人员工具中,当我扩大错误时,我得到:

f.support.ajax.f.ajaxTransport.sendjquery-1.7.1.min.js:4
f.extend.ajaxjquery-1.7.1.min.js:4
$.autocomplete.sourceCreate:217
a.widget._searchjquery-ui-1.8.17.custom.min.js:127
a.widget.searchjquery-ui-1.8.17.custom.min.js:127
(anonymous function)jquery-ui-1.8.17.custom.min.js:127

I placed a breakpoint in the callback function on the server but it doesn't even make it to the server. 我在服务器上的回调函数中放置了一个断点,但它甚至没有到达服务器。 The error is definitely on the client side, here is the client-side code: 该错误肯定是在客户端,这是客户端代码:

$("#LocationTxt").autocomplete({
            minLength: 4,
            source: function (req, resp) {
                $.ajax({
                    type: "GET",
                    url: "/Ad/SearchLocations",
                    data: "term=" + req.term,
                    contentType: "application/json; charset=utf-8",
                    success: function (data) {
                        resp($.map(data, function (value, key) {
                            return { data: value, label: data[key].Name, value: data[key].Name };
                        }));
                    }, 
                   error: function (data) {
                       alert(data.statusText);
                   }
                });
            },
            select: function (e, ui) {
                var cityId = ui.item.data.Id;
                $('#AdListing_LocationID').val(cityId);
            }
        });

Also the error event gets triggered, and the statusText property is simply "error". 此外,还会触发错误事件,并且statusText属性只是“错误”。 Not very helpful. 不是很有帮助。 I am running Chrome version: 17.0.963.46 (I have the latest version as on 2/9/2012). 我正在运行Chrome版本:17.0.963.46(我拥有2012年2月9日的最新版本)。 I believe my Chrome updated this morning when I fired up my PC, but I am not sure. 我相信今天早晨启动PC时,Chrome浏览器已更新,但我不确定。 Is there a log to tell when my chrome was updated? 是否有日志可以告诉我我的Chrome何时更新?

If you are working on a local copy of the code, make sure you are working from within a web-server such as localhost. 如果您正在处理代码的本地副本,请确保您正在从本地主机之类的Web服务器中进行工作。 If you are working directly from the file system, google chrome will not allow you to make ajax requests to files on the file system for security reasons. 如果您直接从文件系统工作,出于安全原因,谷歌浏览器将不允许您向文件系统上的文件发出ajax请求。

A few more things... 还有几件事...

Remove this: 删除此:

contentType: "application/json; charset=utf-8",

You aren't sending json, you are sending a GET request. 您没有发送json,而是发送GET请求。 Instead, add this 相反,添加此

dataType: "json",

so that jQuery expects to receive json. 因此jQuery希望接收json。

It also may help to have your server return headers setting the contentType to application/json with the proper charset utf-8 . 使用适当的字符集utf-8将服务器返回标头将contentType设置为application/json ,这也可能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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