如何在 XAMPP 上启用跨域资源共享? [英] How do I enable cross-origin resource sharing on XAMPP?
问题描述
我的本地主机上有一个 html 文件,其中包含一个表单和处理发布数据的 jquery/ajax.一个简单的php脚本在mysql数据库表中查找数据
这是主要部分:
//$.post('lookup_update.php', $(this).serialize())//<- 有效的本地部分$.post('http://www.example.com/projectX/lookup_update.php', $(this).serialize()).done(function (data){ 等等.
但是当我指向在线 lookup_update.php 时,我在 chrome 中收到以下错误消息
<块引用>XMLHttpRequest 无法加载 http://www.example.com/projectX/lookup_update.php.请求的资源上不存在Access-Control-Allow-Origin"标头.因此,不允许访问源 'http://localhost'.响应的 HTTP 状态代码为 404.
据我所知,我需要使用
header("Access-Control-Allow-Origin: *");
用于 php.但是当我将它添加到 example.com/lookup_update.php 时,当 localhost 文件尝试调用它时,该文件会给出 404.
我还尝试将以下内容添加到我的 Xampp apache 配置文件中
标头设置 Access-Control-Allow-Origin "*"
如何从我的本地 XAMPP 设置中正确启用跨源资源?
[编辑]这是我在本地主机上的简单表单
<div id="form" class="result"><form method="post" id="reg-form" class="form-horizontal"><div class="控件"><input type="text" name="code" id="code" placeholder="Code" class="form-control input-lg"/>
</表单>
<!--结束形式-->
使用以下表单jquery代码
[编辑 2]
好的,我不认为它与在线 lookup_update.php 文件有关,因为我正在使用它在另一个文件中进行测试
var testXHR = $.post("http://www.example.com/projectX/lookup_update.php", function (data) {alert("成功:" + 数据);})
在警报弹出窗口中,我看到了预期的数据
你必须在lookup_update.php的开头写下面的代码
header('Access-Control-Allow-Origin: *');header('内容类型:应用程序/json');
你可以只写IP地址而不是*.
或
首先,您必须检查本地主机或其他服务器上的问题所在.
试试这个代码:如果你在警报中得到一些数据,那么问题出在其他服务器上的本地主机上.
$.post( "test.php", function( data ) {alert( "数据加载:" + 数据);});
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
You have to write below code at the beginning of your lookup_update.php
header('Access-Control-Allow-Origin: *');
header('Content-type: application/json');
Instead of * you can write just Ip address.
OR
First you have to check where is problem either on localhost or other server.
Try this code : If you getting some data in alert then problem is on localhost else on other server.
$.post( "test.php", function( data ) {
alert( "Data Loaded: " + data );
});
这篇关于如何在 XAMPP 上启用跨域资源共享?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!