javascript ajax 请求在 localhost wampserver 中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30765960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 12:40:23  来源:igfitidea点击:

ajax request not working in localhost wampserver

javascriptphpjqueryajaxwampserver

提问by Rakesh Nallam

I have the following code

我有以下代码

$.ajax({
        url: "../profile/companyAutocomplete.php",
        type: "GET",
        data: dataQuery,
        dataType: "json",
        success: function(responseData) {
            companySelectHandler(responseData);
        }
    });

which i getting called in the production server without any issues. But when I used it in my local system, the ajax request is not made. I have tried in all the browsers but its still the same. What would be the causing the issue? I need to print the ajax error message for the same. How can i achieve that?

我在生产服务器中被调用而没有任何问题。但是当我在本地系统中使用它时,没有发出ajax请求。我已经在所有浏览器中尝试过,但它仍然相同。导致问题的原因是什么?我需要打印同样的 ajax 错误消息。我怎样才能做到这一点?

回答by Rakesh Nallam

Thanks for your answers. It was not because of the relative URL reference.

感谢您的回答。这不是因为相对 URL 引用。

I used the following function to figure out which error was causing the Ajax request to fail.

我使用以下函数找出导致 Ajax 请求失败的错误。

$(function() {
    $.ajaxSetup({
        error: function(jqXHR, exception) {
            if (jqXHR.status === 0) {
                alert('Not connect.\n Verify Network.');
            } else if (jqXHR.status == 404) {
                alert('Requested page not found. [404]');
            } else if (jqXHR.status == 500) {
                alert('Internal Server Error [500].');
            } else if (exception === 'parsererror') {
                alert('Requested JSON parse failed.');
            } else if (exception === 'timeout') {
                alert('Time out error.');
            } else if (exception === 'abort') {
                alert('Ajax request aborted.');
            } else {
                alert('Uncaught Error.\n' + jqXHR.responseText);
            }
        }
    });
});

The error was a parse error which was getting generated as the browser was trying to print something else before returning the JSON. It was fixed by using ob_start and ob_end_clean right before outputting the JSON which clears the buffer by getting help from the following link "dataType: "json" won't work"

该错误是一个解析错误,由于浏览器在返回 JSON 之前尝试打印其他内容,因此会生成该错误。它是通过在输出 JSON 之前使用 ob_start 和 ob_end_clean 修复的,该 JSON 通过从以下链接获得帮助来清除缓冲区“ dataType: "json" won't work"

回答by cssyphus

The most common problem is attempting to access the page incorrectly on localhost.

最常见的问题是试图在本地主机上错误地访问页面。

With WAMP, XAMPP, etc you cannot just type in the address bar: c:\website\index.php

使用 WAMP、XAMPP 等,您不能只在地址栏中输入:c:\website\index.php

Instead, you must type: localhost

相反,您必须键入:localhost

See this answer for more info:

有关更多信息,请参阅此答案:

Unable to load assets when testing website localy

本地测试网站时无法加载资产

回答by Xavier

Due to configuration differences between localhost and server, possible that it may be looking in local host at "http://localhost:port/path/file.php" instead of "http://localhost:port/webapproot/path/file.php". Changing your Ajax call in local host prepending web app name in relative path might resolve if it was the issue. Usually on hosting servers "/file.php" refers to application's root and so no issue, but it could be possible in local host that it may be looking at local server root by default since configuration. Better always to use relative path "/webappname/path/file.php", however use "http://127.0.0.1:port/webappname/path/file.php" which pretty faster instead of using "localhost" in local URLs.

由于 localhost 和服务器之间的配置差异,它可能在本地主机中查找“ http://localhost:port/path/file.php”而不是“ http://localhost:port/webapproot/path/file .php”。如果是问题所在,则在相对路径中预先添加 Web 应用程序名称的本地主机中更改 Ajax 调用可能会解决。通常在托管服务器上,“/file.php”指的是应用程序的根目录,因此没有问题,但在本地主机中,自配置以来,它可能会默认查看本地服务器根目录。最好总是使用相对路径“/webappname/path/file.php”,但是使用“ http://127.0.0.1:port/webappname/path/file.php”,这比在本地 URL 中使用“localhost”要快得多.

回答by user3072843

You should put the URL instead of a relative path, e.g url: "http://localhost/something/profile/companyAutocomplete.php".

您应该输入 URL 而不是相对路径,例如 url:“ http://localhost/something/profile/companyAutocomplete.php”。

You can also omit the domain name, starting with "/something/profile/companyAutocomplete.php".

您也可以省略域名,以“/something/profile/companyAutocomplete.php”开头。