访问控制 - 允许 - 原产地错误发送的jQuery发布到谷歌的API [英] Access-Control-Allow-Origin error sending a jQuery Post to Google API's

查看:140
本文介绍了访问控制 - 允许 - 原产地错误发送的jQuery发布到谷歌的API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读了很多的访问控制 - 允许 - 原产地的错误,但我不明白我必须解决:(

我在和谷歌主持人API,但是当我尝试增加新意甲我收到:

  XMLHtt prequest无法加载
https://www.googleapis.com/moderator/v1/series?key=[key]
&放大器;数据%5Bdescription%5D =分享+和+等级+为+饮食+健康+上+上+廉价货物技巧+!
&放大器;数据%5Bname%5D =吃+健康+%26 +特价
&放大器;数据%5BvideoSubmissionAllowed%5D =假的。
产地[my_domain]不受访问控制 - 允许 - 原产地允许的。
 

我试过有和没有回​​调参数,我想'访问控制 - 允许 - 产地*添加到头部。我不知道如何使用$ .getJSON这里,如果适用,因为我要补充授权头和我不知道如何从$就做没有beforeCall:/

在任何光线下这片黑暗u.u?

这就是code:

 <脚本SRC =htt​​p://www.google.com/jsapi>< / SCRIPT>

<脚本类型=文/ JavaScript的>

VAR范围=htt​​ps://www.googleapis.com/auth/moderator;
VAR令牌='';

函数来创建(){
     如果(令牌=='')
      令牌= doCheck();

     VAR的myData = {
      数据: {
        说明:共享和排名技巧上的廉价货物健康吃!,
        名:吃的健康和安培;价格便宜,
        videoSubmissionAllowed:假的
      }
    };

    $阿贾克斯({

        网址:'https://www.googleapis.com/moderator/v1/series?key='+key,
        键入:POST,
        回电话: '?',
        数据:myData的,
        数据类型:应用/ JSON的,
        成功:函数(){警报(成功); },
        错误:函数(){警报(失败!); },
        beforeSend:SetHeader可以

    });
}

功能SetHeader可以(XHR){

  xhr.setRequestHeader('授权',令牌);
}

功能doLogin(){
    如果(令牌==''){
       令牌= google.accounts.user.login(范围);
    }其他{
       警报(已登录);
    }
}


传播doCheck(){
    令牌= google.accounts.user.checkLogin(范围);
    返回令牌;
}
< / SCRIPT>
...
...
< D​​IV数据角色=内容>
    <输入类型=按钮值=登陆的onclick =doLogin();>
    <输入类型=按钮值=获取数据的onclick =getModerator();>
    <输入类型=按钮值=创建的onclick =创建();>
< / DIV><! -  /内容 - >
 

解决方案

我解决了访问控制 - 允许 - 产地的错误修改dataType参数来的数据类型:JSONP的并增加一个跨域:真正的

  $。阿贾克斯({

    网址:'https://www.googleapis.com/moderator/v1/series?key='+key,
    数据:myData的,
    键入:GET,
    跨域:真正的,
    数据类型:JSONP,
    成功:函数(){警报(成功); },
    错误:函数(){警报(失败!); },
    beforeSend:SetHeader可以
});
 

I read a lot for the 'Access-Control-Allow-Origin' error, but I don't understand what I have to fix :(

I'm playing with Google Moderator API, but when I try to add new serie I receive:

XMLHttpRequest cannot load 
https://www.googleapis.com/moderator/v1/series?key=[key]
&data%5Bdescription%5D=Share+and+rank+tips+for+eating+healthily+on+the+cheaps!
&data%5Bname%5D=Eating+Healthy+%26+Cheap
&data%5BvideoSubmissionAllowed%5D=false. 
Origin [my_domain] is not allowed by Access-Control-Allow-Origin.

I tried with and without callback parameter, I tried to add 'Access-Control-Allow-Origin *' to the header. And I don't know how to use $.getJSON here, if apply, because I have to add the Authorization header and I don't know how to do it without beforeCall from $.ajax :/

Any light for this darkness u.u?

That's the code:

<script src="http://www.google.com/jsapi"></script>

<script type="text/javascript">

var scope = "https://www.googleapis.com/auth/moderator";
var token = '';

function create(){
     if (token == '')
      token = doCheck();

     var myData = {
      "data": {
        "description": "Share and rank tips for eating healthily on the cheaps!", 
        "name": "Eating Healthy & Cheap", 
        "videoSubmissionAllowed": false
      }
    };

    $.ajax({

        url: 'https://www.googleapis.com/moderator/v1/series?key='+key,
        type: 'POST',
        callback: '?',
        data: myData,
        datatype: 'application/json',
        success: function() { alert("Success"); },
        error: function() { alert('Failed!'); },
        beforeSend: setHeader

    });
}

function setHeader(xhr) {

  xhr.setRequestHeader('Authorization', token);
}

function doLogin(){ 
    if (token == ''){
       token = google.accounts.user.login(scope);
    }else{
       alert('already logged');
    }
}


function doCheck(){             
    token = google.accounts.user.checkLogin(scope);
    return token;
}
</script>
...
...
<div data-role="content">
    <input type="button" value="Login" onclick="doLogin();">
    <input type="button" value="Get data" onclick="getModerator();">
    <input type="button" value="Create" onclick="create();">
</div><!-- /content -->

解决方案

I solved the Access-Control-Allow-Origin error modifying the dataType parameter to dataType:'jsonp' and adding a crossDomain:true

$.ajax({

    url: 'https://www.googleapis.com/moderator/v1/series?key='+key,
    data: myData,
    type: 'GET',
    crossDomain: true,
    dataType: 'jsonp',
    success: function() { alert("Success"); },
    error: function() { alert('Failed!'); },
    beforeSend: setHeader
});

这篇关于访问控制 - 允许 - 原产地错误发送的jQuery发布到谷歌的API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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