ajax跨域在jQuery中不起作用 [英] ajax cross domain not working in jquery

查看:53
本文介绍了ajax跨域在jQuery中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

错误:

对预检请求的响应未通过访问控制检查:"Access-Control-Allow-Origin"标头包含多个值" http ://localhost:8080 ,*',但只允许一个.因此,不允许访问来源' http://localhost:8080 .

Response to preflight request doesn't pass access control check: The 'Access-Control-Allow-Origin' header contains multiple values 'http://localhost:8080, *', but only one is allowed. Origin 'http://localhost:8080' is therefore not allowed access.

$("#selector3").autocomplete({
            source: function(request, response) {               

                $.ajax({
                   url: "http://example.com/"+$("#selector3").val(),
                    type: "GET",
                        dataType: "json",
                        data: request,
                        processData: true,
                        data: {},

                        headers: { 
                            "Access-Control-Allow-Origin" : "*",
                            "Access-Control-Allow-Headers": "origin, content-type, accept"
                                    },

                        crossDomain: true,
                    success: function(data) {   
                        alert(data.Company_Id);
});

推荐答案

请使用 JSONP 跨域脚本.

检查以下示例代码:

$.ajax({
    url: "http://example.com/" + $("#selector3").val(),
    type: "GET",
    dataType: 'jsonp', // Notice! JSONP <-- P (lowercase)
    data: request,
    processData: true,
    data: {},
    headers: {
        "Access-Control-Allow-Origin": "*",
        "Access-Control-Allow-Headers": "origin, content-type, accept"
    },
    success: function(data) {
        alert(data.Company_Id);
    }
});

这篇关于ajax跨域在jQuery中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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