不能得到CORS工作在chrome和firefox [英] cant get CORS working in chrome and firefox

查看:329
本文介绍了不能得到CORS工作在chrome和firefox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您尝试对ajax跨域调用,但是有标题的问题。我按照许多教程,但我只是不能得到它的工作。

Hello im trying to ajax cross domain call , but there is problem with headers. I followed many of tutorials but i just cant get it working.

这是我走了多远:

<html>
<head>
<meta http-equiv="Access-Control-Allow-Origin" content="*">
</head>
<body>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js" ></script>

<script>

    jQuery.support.cors = true;

    $(document).ready(function() {

        //this would be done in a common script file if you are going to
        //make a lot of these calls
        $.ajaxSetup({ type: 'POST', dataType: 'json', contentType: 'application/json', data: {} });

        $('#btn').click(function() {
             //call the web service with the button is clicked
            $.ajax({ url: 'url_external',
                data: '{ "some_data:data" }', //ensure the data is enclosed in a quote
                            success: function (data) {
                alert('here...success');
            },
            complete: function (data) {
                alert('here');
            }
            });

            //return false to avoid a postback
            return false;
        });

    });
</script>


<div>
    <button id='btn'>Load It</button>
    <div id='sayit'></div>
</div>
</body>
</html>

这只适用于IE9。我缺少什么?在Mozilla Firefox和Google Chrome中,有与选项和不允许方法

And this only work in IE9. Am I missing something? In Mozilla Firefox and Google Chrome there is problem with options and 405 Method not allowed

推荐答案

'url_external'服务器上的所有HTTP响应:

Add this to all of your HTTP responses on the 'url_external' server:

<?php

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE');
header('Access-Control-Allow-Headers', 'Content-Type');

这将启用

This enables CORS Ajax requests on your url_external server.

您的 url_external 服务器上的origin_resource_sharingrel =nofollow> CORS d可能希望将此微调为...

You'd probably want to fine tuned this to...

header('Access-Control-Allow-Origin: http://yourdomain.com');

...仅允许来自您的网域的CORS请求。

... in order to allow only CORS requests coming only from your domain.

我找到的关于CORS的最佳信息:使用CORS a>。

The best information I found about CORS: Using CORS.

这篇关于不能得到CORS工作在chrome和firefox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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