陷阱对象要求:MSXML2.ServerXMLHTTP.6.0中的"[object]" [英] Trapping Object required: '[object]' with MSXML2.ServerXMLHTTP.6.0
问题描述
我正在使用Flickr API和MSXML2.ServerXMLHTTP.6.0来读取XML响应.
I am using the Flickr API and MSXML2.ServerXMLHTTP.6.0 to read the XML response.
例如:
<rsp stat="ok">
<photos page="1" pages="847622" perpage="1" total="847622">
<photo id="8658999887" owner="46248160@N04" secret="13e2615193" server="8123" farm="9" title="cheeses" ispublic="1" isfriend="0" isfamily="0"/>
</photos>
</rsp>
我做了一些简单的ASP来设置变量值:
I do some simple ASP to set variable values:
vurl = https://www.flickr.com/services/rest/?method=flickr.photos.search&api_key=[mykey]&text=cheese&per_page=1&sort=relevance&min_upload_date=2010-05-25
Set http = Server.CreateObject("MSXML2.ServerXMLHTTP.6.0")
http.Open "GET", vurl, False
http.Send
Set dom = http.responseXML
Set items = dom.getElementsByTagName("photos")
If NOT (items IS Nothing) Then
For Each item In items
Set tags = item.getElementsByTagName("photo")
If NOT (tags IS Nothing) Then
var_id = tags(0).getAttribute("id")
var_owner = tags(0).getAttribute("owner")
var_server = tags(0).getAttribute("server")
var_farm = tags(0).getAttribute("farm")
var_title = tags(0).getAttribute("title")
var_public = tags(0).getAttribute("public")
var_secret = tags(0).getAttribute("secret")
var_img_url2 = "https://farm"&var_farm&".staticflickr.com/"&var_server&"/"&var_id&"_"&var_secret&"_b.jpg"
var_id = ""
%>
<%
End If
Next
end if
我的问题是,如果API没有针对搜索字词返回任何图片,例如"aplogranite"的XML响应是:
My problem is that if there is no image returned by the API for a search term - e.g. "aplogranite" the XML response is:
<rsp stat="ok">
<photos page="1" pages="0" perpage="1" total="0"/>
</rsp>
我上面的代码返回此错误:
My code above returns this error:
Microsoft VBScript runtime error '800a01a8'
Object required: '[object]'
我尝试通过以下方式捕获它:
I tried trapping it via:
Set items = dom.getElementsByTagName("photos")
If NOT (items IS Nothing) Then...
但是它仍然是错误的.
我想知道如何捕获该错误,以便处理XML在照片"标签内不包含任何数据的情况?
I wondered how I could trap the error so that I can handle the scenario where the XML does not contain any data within the "photo" tag?
推荐答案
在没有元素匹配的情况下, getElementsByTagName()
似乎返回了空集合,而不是 Nothing
.标签名称参数.因此,请尝试检查返回值的 Length
:
Seems that instead of Nothing
, getElementsByTagName()
returns empty collection in the case when no element match tag name parameter. So try to check Length
of the return value instead :
If tags.Length > 0 Then
....
End If
或者测试第一个元素是否为 Nothing
:
Or maybe test the first element for Nothing
:
If Not (tags(0) Is Nothing) Then
....
End If
这篇关于陷阱对象要求:MSXML2.ServerXMLHTTP.6.0中的"[object]"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!