如何发送跨域ajax请求 [英] How to send a cross domain ajax request

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

问题描述

我想发送一个跨域 ajax 请求,但我遇到了以下错误>

我尝试发送跨域请求的代码.

$(document).ready(function () {$.ajax({类型:'POST',url: "http://testdomain.com/config.php",跨域:真,数据:'rejected_by_loid='+ 1,成功:功能(味精){$('#survey').html(msg);}});});

控制台日志错误:

跨源请求被阻止:同源策略不允许读取 http://beta.paravey.com/paraveyads/config.php.(原因:缺少 CORS 标头Access-Control-Allow-Origin").

请帮我解决这个问题.

解决方案

放在 config.php 之上

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

I want to send a cross domain ajax request but I am getting below error for this I have also tried the below code which I have got from the one stack article is it possible to send a cross domain request or not.

Code which I have tried for sending cross domain request.

$(document).ready(function () {
    $.ajax({
        type: 'POST',
        url: "http://testdomain.com/config.php",
        crossDomain: true,
        data: 'rejected_by_loid=' + 1,
        success: function (msg) {
            $('#survey').html(msg);
        }
    });
});

Error on console log:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://beta.paravey.com/paraveyads/config.php. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).

Please help me to solve this problem.

解决方案

put it on top of config.php

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

这篇关于如何发送跨域ajax请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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