如何在服务器上启用跨域请求? [英] How to enable cross-domain request on the server?

查看:187
本文介绍了如何在服务器上启用跨域请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的托管服务器上的JSON文件。当我尝试做一个AjaxGET请求JSON文件,它失败。

请参阅Safari中的控制台,它说:无法加载的资源。

萤火虫显示200 OK,但响应显示不出来。甚至萤火虫不显示的JSON标签

我相信这是因为使用AJAX跨域请求是不允许的。

我想知道我怎么能解决这个?另外,如果我想使我的服务器上的跨域请求,我相信的crossdomain.xml 文件或东西需要创建。我不知道,但这是我所知道的。我搜索谷歌,但找不到任何相关的链接。

任何帮助,这是非常AP preciated。

感谢。

更新: 我没有使用任何服务器端脚本语言(PHP,ASP.NET等)。我使用普通的HTML和JavaScript / jQuery的。

更新-2:

我用下面的code键使跨域请求:

 <脚本SRC =jQuery的-1.6.2.js>< / SCRIPT>
  <脚本>
  $(文件)。就绪(函数(){
    $阿贾克斯({
      数据类型:JSONP,
      数据: '',
      JSONP:jsonp_callback,
      网址:http://myhosting.net/myjsonfile.json,
      成功:函数(jsonData){
        警报(成功)
        警报(jsonData);
      },
      错误:函数(errorObj){
        警报(errorObj.statusText);

      },
    });
});
 

当我在Firebug的网络选项卡上看到,我看到一个JSON选项卡,我能看到JSON响应。然而,成功回调处理程序不会被调用,但错误回调处理程序被调用,我得到了警告,指出 parseerror

任何想法可能是错误的?

解决方案

 访问控制 - 允许 - 产地:http://yourdomain-you-are-connecting-from.com
 

在目标服务器上

在PHP中:

 标题(访问控制 - 允许 - 产地:http://yourdomain-you-are-connecting-from.com);
 

如果你不想使用服务器脚本语言:把这个(Linux)的控制台

  a2enmod头
 

和.htaccess文件添加

 标题设置访问控制 - 允许 - 产地:http://yourdomain-you-are-connecting-from.com
 

I have a json file hosted on my server. When I try to make an Ajax "GET" request to the json file, it fails.

See the console in Safari, it says "Failed to load resource".

Firebug shows "200 OK", but the response doesn't show up. Even Firebug does not show the JSON tab.

I believe this is because Cross Domain Requests are not allowed using AJAX.

I would like to know how can I overcome this? Also, if I want to enable cross-domain requests on my server, I believe a crossdomain.xml file or something needs to be created. I am not sure, but this is what I know. I searched on Google, but could not find any relevant links.

Any help in this is highly appreciated.

Thanks.

UPDATE: I am not using any server-side scripting language (PHP, ASP.NET, etc). I am using Plain HTML and JavaScript / jQuery.

UPDATE-2:

I used the following code to make cross-domain requests:

<script src="jquery-1.6.2.js"></script>
  <script>
  $(document).ready(function () {
    $.ajax({
      dataType: 'jsonp',
      data: '',
      jsonp: 'jsonp_callback',
      url: 'http://myhosting.net/myjsonfile.json',
      success: function (jsonData) {
        alert("success")
        alert(jsonData);
      },
      error: function(errorObj) {
        alert(errorObj.statusText);

      },
    });
});

When i see in Firebug's "Net" tab, I see a JSON tab, and I am able to see the json response. However, the "success" callback handler doesn't get called, but the "error" callback handler gets invoked and I get the alert saying parseerror.

Any idea what could be wrong?

解决方案

Access-Control-Allow-Origin: http://yourdomain-you-are-connecting-from.com

on target server

in php:

 header("Access-Control-Allow-Origin: http://yourdomain-you-are-connecting-from.com");

in case you don't want to use server-scripting language: put this in (linux) console

a2enmod headers

and to your .htaccess file add ­ ­ ­ ­ ­ ­ ­

Header set Access-Control-Allow-Origin: http://yourdomain-you-are-connecting-from.com

这篇关于如何在服务器上启用跨域请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆