首页 \ 问答 \ 如何在XAMPP上启用跨源资源共享?(How do I enable cross-origin resource sharing on XAMPP?)

如何在XAMPP上启用跨源资源共享?(How do I enable cross-origin resource sharing on XAMPP?)

我的localhost上有一个带有表单和jquery / ajax的html文件,用于处理发布数据。 一个简单的php脚本在mysql数据库表中查找数据

这是主要部分:

// $.post('lookup_update.php', $(this).serialize()) //<- local part which works
   $.post('http://www.example.com/projectX/lookup_update.php', $(this).serialize()).done(function (data)
                            { etc.

但是,当我指向在线lookup_update.php时,我在chrome中收到以下错误消息

XMLHttpRequest无法加载http://www.example.com/projectX/lookup_update.php 。 请求的资源上不存在“Access-Control-Allow-Origin”标头。 因此不允许Origin'http:// localhost '访问。 响应具有HTTP状态代码404。

据我了解,我需要使用

 header("Access-Control-Allow-Origin: *");

对于PHP。 但是当我将其添加到example.com/lookup_update.php时,当localhost文件尝试调用它时,该文件会给出404。

我还尝试将以下内容添加到我的Xampp apache配置文件中

Header set Access-Control-Allow-Origin "*"

如何从本地XAMPP设置中正确启用跨源资源?

[ 编辑 ]这是我在localhost上的简单表单

<!--Begin form-->
    <div id="form" class="result">
        <form method="post" id="reg-form"  class="form-horizontal">
            <div class="controls">
                <input type="text" name="code" id="code" placeholder="Code"  class="form-control input-lg" />      
            </div>
        </form>
    </div>
    <!--End form-->

用以下形式的jquery代码

    <script type="text/javascript">
            $(document).ready(function ()
            {
                $(document).on('submit', '#reg-form', function ()
                {
                    var tmpCode = $("#code").val();

//                    $.post('lookup_update.php', $(this).serialize())
                      $.post('http://www.example.com/projectX/lookup_update.php', $(this).serialize())
                            .done(function (data)
                            {

                                $("#reg-form").fadeOut('slow', function ()
                                {
                                    $(".result").fadeIn('slow', function ()
                                    {
                                        console.log("inner test " + tmpCode);
                                        $(".result").html(data);


                                        setTimeout(function () {
                                            location.reload();
                                            $('input').val("");
                                        }, 3000);


                                    });
                                });
                            })
                            .fail(function ()
                            {
                                alert('fail to submit the data');
                            });
                    return false;
                });


            });

        </script>

[ 编辑2 ]

好的,我不认为它与在线lookup_update.php文件有关,因为我用它在另一个文件中测试

            var testXHR = $.post("http://www.example.com/projectX/lookup_update.php", function (data) {
            alert("success:" + data);
        })

在警报弹出窗口中,我看到了预期的数据


I have a html file on my localhost with a form and jquery/ajax which handles the post data. A simple php script looks up the data in a mysql database table

This is the main part:

// $.post('lookup_update.php', $(this).serialize()) //<- local part which works
   $.post('http://www.example.com/projectX/lookup_update.php', $(this).serialize()).done(function (data)
                            { etc.

But when I point to the online lookup_update.php I get the following error message in chrome

XMLHttpRequest cannot load http://www.example.com/projectX/lookup_update.php. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access. The response had HTTP status code 404.

As I understand it I need to use

 header("Access-Control-Allow-Origin: *");

for php. But when I add this to example.com/lookup_update.php, the file gives a 404 when the localhost file tries to call it.

I also tried to add the following to my Xampp apache config file

Header set Access-Control-Allow-Origin "*"

How do I correctly enable cross-origin resource from my local XAMPP setup??

[EDIT] This is my simple form on my localhost

<!--Begin form-->
    <div id="form" class="result">
        <form method="post" id="reg-form"  class="form-horizontal">
            <div class="controls">
                <input type="text" name="code" id="code" placeholder="Code"  class="form-control input-lg" />      
            </div>
        </form>
    </div>
    <!--End form-->

With the following form jquery code

    <script type="text/javascript">
            $(document).ready(function ()
            {
                $(document).on('submit', '#reg-form', function ()
                {
                    var tmpCode = $("#code").val();

//                    $.post('lookup_update.php', $(this).serialize())
                      $.post('http://www.example.com/projectX/lookup_update.php', $(this).serialize())
                            .done(function (data)
                            {

                                $("#reg-form").fadeOut('slow', function ()
                                {
                                    $(".result").fadeIn('slow', function ()
                                    {
                                        console.log("inner test " + tmpCode);
                                        $(".result").html(data);


                                        setTimeout(function () {
                                            location.reload();
                                            $('input').val("");
                                        }, 3000);


                                    });
                                });
                            })
                            .fail(function ()
                            {
                                alert('fail to submit the data');
                            });
                    return false;
                });


            });

        </script>

[EDIT 2]

OK, i don't think it has to do with the online lookup_update.php file, as I am using this to test in another file

            var testXHR = $.post("http://www.example.com/projectX/lookup_update.php", function (data) {
            alert("success:" + data);
        })

And in the alert popup window I see the expected data


原文:https://stackoverflow.com/questions/34872760
更新时间:2024-03-01 21:03

最满意答案

您的共享代码中存在两个错误:

  1. 比较字符串时总是使用ToLower()因为单词Liza或LIZA不等于liza。
  2. 你在第二个代码块中使用了username =“liza”,这是不正确的,因为在这里你指的不是比较。

<%if username.ToLower() <> "liza" then%>
    <a href="a.asp" class="white-text"> Delivery Record (Self collect) </a>
<%end if%>

<%if username.ToLower() == "liza"  then%>
    <a href="b.asp" class="white-text"> Delivery Record (Self collect) </a>
<%end if%>

此外,您可以避免两个条件if并将它们作为一个。

<%if username.ToLower() <> "liza" then%>
    <a href="a.asp" class="white-text"> Delivery Record (Self collect) </a>
<%else%>
    <a href="b.asp" class="white-text"> Delivery Record (Self collect) </a>
<%end if%>

There are two mistakes in your shared code:

  1. When comparing a string always use ToLower() because word Liza or LIZA is not equal to liza.
  2. You have used username = "liza" in second code block that incorrect because here you are assigning not comparing.

Solution

<%if username.ToLower() <> "liza" then%>
    <a href="a.asp" class="white-text"> Delivery Record (Self collect) </a>
<%end if%>

<%if username.ToLower() == "liza"  then%>
    <a href="b.asp" class="white-text"> Delivery Record (Self collect) </a>
<%end if%>

Also, you can avoid two conditional if case and make them one.

<%if username.ToLower() <> "liza" then%>
    <a href="a.asp" class="white-text"> Delivery Record (Self collect) </a>
<%else%>
    <a href="b.asp" class="white-text"> Delivery Record (Self collect) </a>
<%end if%>

相关问答

更多
  • 您需要初始化整个框架才能访问其部件。 以主index.php为例说明如何做到这一点。 而不是路由和调度,将代码放在那里。 独立-的script.php
  • 您只能检测是否在页面加载时启用/禁用JavaScript。 在页面加载后没有要求启用/禁用的事件。 我认为唯一可行的解决方案是在主页面中包含一个隐含的iframe,其中包含一个带有小元刷新的脚本,并检查是否启用了JavaScript - 如果是,则重定向父(主)页面。 所以你的iframe会包含这样的内容: jscheck.html my iframe