操作更新img.src导致的http请求

时间:2012-06-06 22:06:45

标签: javascript html5

我目前正在为iOS和Android创建一个混合移动应用程序(请参阅phonegap / cordova),并注意到更新图像的img.src网址(我经常这样做)时,Android http请求如下所示。

我的问题是它不包含所有重要的Accept标头(Accept: / ),因此服务器无法加载图像并返回(HTTP / 1.1 406 Not Acceptable)。更新img.src网址时,Chrome / iOS会在其http请求中包含此Accept标头。

我的问题是,有没有办法添加此标头或某些包含此标头以供后续的img.src更新?

Android Http请求:

GET /system/data/ba9320b8-e093-47a9-8858-c6343febf3ec/frame?t=1339017043002 HTTP/1.1
Host: MyHostName
Connection: keep-alive
User-Agent: Mozilla/5.0 (Linux; U; Android 4.0.2; en-us; Galaxy Nexus Build/ICL53F)
AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30
Accept-Encoding: gzip,deflate
Accept-Language: en-US
Accept-Charset: utf-8, iso-8859-1, utf-16, *;q=0.7
Cookie: auth_token=0882f24f-04d7-4f05-9475-cfe2a94af5bf

1 个答案:

答案 0 :(得分:1)

查看Google implementation以解决所有此类问题的兼容性问题。这是来自Closure Library,它与PhoneGap兼容(至少他们说:)

基本上您需要做的是更改http请求的标头。本机xhr对象有一个名为setRequestHeader(param,value)的方法;这就是你要找的东西。使用remote debugging on Android查看已发送的标头以及位置。

我不知道您使用的是哪个库。使用Google Closure库,您可以将请求标头作为对象参数传递给xhr实例(SEE THIS)的send方法,因此非常简单。据我所知,jQuery也有类似的行为。

$.ajax({
         url: "http://someurl",
         data: { signature: authHeader },
         type: "GET",
         beforeSend: function(xhr){xhr.setRequestHeader('X-Test-Header', 'test-value');}
         success: function() { alert('Success!' + authHeader); }
      });