jQuery Ajax 未经授权的 401 错误 [英] jQuery Ajax Unauthorized 401 Error

查看:123
本文介绍了jQuery Ajax 未经授权的 401 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码:

<?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 时看到的示例输出

I'm getting a 401 Unauthorized error when using ajax but when I try to use an ordinary POST method (non-ajax), then I am able to see the output.
This is an exmaple ouput that i'm seeing when using an ordinary POST

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

顺便说一下,我正在我的应用程序中集成谷歌阅读器帐户,这就是我在 ajax-url 中调用该帐户的原因.

By the way i'm integrating the google reader account in my application that is why i'm calling that one in the ajax-url.

推荐答案

这应该对你有帮助

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);
            }
        });

这篇关于jQuery Ajax 未经授权的 401 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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