问题描述
我正在使用jQuery Mobile,jQuery 1.7.2和PhoneGap构建iPhone应用程序
I am building an iPhone app using jQuery Mobile, jQuery 1.7.2, and PhoneGap
尝试使用此代码从ASP.NET RESTful Web服务获取JSONP,这是我需要先进行身份验证并获得令牌,然后再次将其发送回去的问题.
trying to get a JSONP from ASP.NET RESTful web service using this code, problem that I need to authenticate first and get a token, then send it back again.
这是我的功能:
var setToken = function () {
var serverToken = '';
$.support.cors = true;
jQuery('body').ajaxSend(function (evt, xhr) {
xhr.setRequestHeader("Authorization", "Basic " + $.base64.encode(username + ":" + password));
xhr.setRequestHeader('X-Accept', 'application/json');
});
$.getJSON(url + "?jsoncallback=?", null, function (res) {
console.log(res);
serverToken = res.Token;
});
$('body').ajaxSend(function (evt, xhr, ajaxOptions) {
xhr.setRequestHeader('NewToken', serverToken);
});
};
我得到401(未经授权)检查标题:
I get 401 (Unauthorized)checked the header:
Request URL:http://192.168.0.44/call/?jsoncallback=jQuery17206244203052483243499_132336710607307&_=1336742104278
Request Method:GET
Status Code:401 Unauthorized
Request Headers
Accept:*/*
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Host:192.168.0.44
Referer:http://localhost/myMobileApp/
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.168 Safari/535.19
Query String Parametersview URL encoded
jsoncallback:jQuery17206244203052483243499_132336710607307
_:1336710704278
Response Headers
Access-Control-Allow-Origin:*
Cache-Control:private
Content-Length:0
Date:Fri, 11 May 2012 04:31:52 GMT
Server:Microsoft-IIS/7.5
WWW-Authenticate:Basic
X-AspNetMvc-Version:3.0
X-Powered-By:ASP.NET
setRequestHeader没有添加标题.
setRequestHeader did not add the headers.
我在做什么错了?
请记住,服务器端已经设置为返回JSONP,我认为我们不需要在那里进行更改,但是任何想法都会有所帮助
keep in mind that the server side was set up already to return JSONP, I don't think we need to make changes there, but any thoughts would help
预先感谢
推荐答案
如jfriend00
所述,
但是,在您的情况下(您要添加的标头是基本授权),您可以像通常那样将其注入URL中.因此,要在 http://example.com 上使用用户"user"和密码"pass"进行身份验证,URL将为 http://user:[email protected]/.
However in your case - the header you're attempting to add being basic Authorization - you can just inject that into the URL, as you'd "usually" do. So to authenticate at http://example.com with the user "user" and password "pass", the URL would be http://user:[email protected]/.
这篇关于setRequestHeader在使用jQuery的JSONP中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!