AJAX 调用适用于本地计算机,不适用于主机

发布于 2024-08-29 18:35:15 字数 174 浏览 8 评论 0原文

当 AJAX 调用在本地主机上正常工作时,什么可能会阻止 AJAX 调用在主机服务器上工作?我尝试从 AJAX 调用返回错误,但得到的只是“未定义”。我不认为实际的页面方法被调用,因为我的日志中没有添加任何信息(并且我已经明确添加了一个调用)。我无法在本地计算机上重现该问题,所以有人知道我应该调查的可能区域吗?

谢谢

What could potentially stop an AJAX call from working on the host server, when it works fine on the local host? I tried returning an error from the AJAX call, but all I get is 'undefined'. I don't think the actual page method is being called since no information is added in my log (and I've explicitly added a call). I can't reproduce the problem on my local machine, so does anyone know possible areas I should look into?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尐籹人 2024-09-05 18:35:15

您的浏览器中的 URL 是什么?您的 Ajax 调用点击了哪个 URL?浏览器有“安全”限制,不允许跨域 AJAX 调用。例如,如果我正在加载本地文件:

http://localhost:20080/myCustomPage.html

在该页面中,我进行 ajax 调用,

http://search.twitter.com/search?q=test

我会从浏览器收到安全警告。根据您的浏览器,这可能会在底角 (IE) 中显示警告图标,或在 javascript 日志 (firefox) 中显示错误。

在一种特定情况下有一种方法可以解决这个问题。这种情况是任何 GET 请求。要执行此操作,而不是进行“ajax”调用,您需要包含一个标签。然后将读取并加载该脚本标签。这里的问题是,现在的调用需要包含一个“回调”方法,您可以在自己这边实现该方法,并使用调用的结果来调用该方法。

此示例使用 JSON,而不是像这样的响应

{
    "first_name": "peter",
    "last_name": "parker"
}

因此,您需要返回

myCallBackFunction({
    "first_name": "peter",
    "last_name": "parker"
});

,但只要调用该函数,您就可以轻松使用 XML、HTML 或任何其他结果格式。

myCallbackFunction("INSERT RESPONSE TEXT HERE")

此方法通常被称为 JSONP ,幸运的是,它在常见的 javascript 库中实现,例如jquery从客户端角度。如果您控制服务器端,则需要硬编码 callBackFunction 包装器,或公开允许客户端设置它的参数。不幸的是,如果您不拥有该库,那么您无能为力,除非该服务的所有者已经提供了该功能。幸运的是,您要做的大多数 Web 2.0 服务都已经实现了该功能。

What URL is in your Browser, and What URL is your Ajax call hitting? Browsers have 'security' constraints that don't allow cross domain AJAX calls. So for instance, if I am loading a local file:

http://localhost:20080/myCustomPage.html

and within that page, I make an ajax call to

http://search.twitter.com/search?q=test

I would get a security warning from the browser. Depending on your browser this may manifest itself in a warning icon in the bottom corner (IE), or an error in the javascript log (firefox).

There is a way to get around this in ONE specific case. That case being any GET request. To do this instead of making an 'ajax' call you include a tag. That script tag will then be read and loaded. The catch here, is the call now needs to include a "callback" method, that you can implement on your side, that gets called with the result of the call.

So instead of a response like:

{
    "first_name": "peter",
    "last_name": "parker"
}

you would need to return

myCallBackFunction({
    "first_name": "peter",
    "last_name": "parker"
});

This example is using JSON, but you could easily use XML, HTML or any other result format as long as the function is called.

myCallbackFunction("INSERT RESPONSE TEXT HERE")

This method is commonly refereed to as JSONP and is fortunately implemented in the common javascript libraries like jquery from the client perspective. If you control the server side, you will need to hard code a callBackFunction wrapper, or expose a parameter that allows the client to set it. And unfortunately if you don't own the library there isn't much you can do unless the owner of the service already provides that feature. Fortunately most Web 2.0 services you would be doing stuff like this, already implement that feature.

面如桃花 2024-09-05 18:35:15

我已经设法修复它。在集成模式下运行 IIS7 时出现问题。我将所有模块从 移至 并移至(也在该部分中)

希望对其他人有帮助。

详细信息

I've managed to fix it. There was an issue with running IIS7 in integrated mode. I moved all my modules from to and my to (also in the section)

Hope that helps anyone else.

More info

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文