jQuery Ajax未经授权的401错误

时间:2012-01-13 08:27:25

标签: php javascript jquery api google-reader

我有这段代码:

<?php
$username = $_POST['username'];
$password = $_POST['password'];
$ck = time();
$read = new GoogleReaderAPI( $username, $password );
$token = $read->getToken();

echo "RSS Feed: <input type='text' name='rss_feed' /> <br /> ";
echo "<input type='hidden' value='$token' id='token' />";
echo "<input type='hidden' value='$ck' id='ck' />";
echo "<input type='hidden' value='$username' id='username' />";
echo "<input type='hidden' value='$password' id='password' />";
echo '<input type="button" value="Submit" id="click"/>';
echo "<div id='result'></div>";
?>
<script>
jQuery("#click").click(function(){
    var quickadd = escape( jQuery("input[name=rss_feed]").val() );
    var T = escape( jQuery("#token").val() );
    var ck = escape( jQuery("#ck").val() );
    var params = "quickadd="+quickadd+"&T="+T;

        jQuery.ajax({
            type: "POST",
            url: "http://www.google.com/reader/api/0/subscription/quickadd?ck="+ck+"&client=scroll",
            data: params,
            contentType: "application/json",
            success: function(data){
                alert(data);
            }
        });
    });

我在使用ajax时遇到401 Unauthorized错误,但是当我尝试使用普通的POST方法(非ajax)时,我能够看到输出。
这是我在使用普通POST时看到的一个例子

{"query":"http://feeds.feedburner.com/TechCrunchIT","numResults":1,"streamId":"feed/http://feeds.feedburner.com/TechCrunchIT"}

顺便说一句,我在我的应用程序中集成谷歌阅读器帐户,这就是为什么我在ajax-url中调用那个。

1 个答案:

答案 0 :(得分:0)

这应该可以帮到你

jQuery.ajax({
            type: "POST",
            url: "http://www.google.com/reader/api/0/subscription/quickadd?ck="+ck+"&client=scroll",
            data: params,
            contentType: "application/json",
            beforeSend: function(xhr) {
                 xhr.setRequestHeader("Authentication", "Basic " + encodeBase64(username + ":" + password) //May need to use "Authorization" instead
            },
            success: function(data){
                alert(data);
            }
        });
相关问题