Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ajax, xml results and ie error "object required"

ajax, xml results and ie error "object required"

Scheduled Pinned Locked Moved Web Development
helpdatabasetestingbeta-testingtools
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    cjoki
    wrote on last edited by
    #1

    Hello all, This is more of a posting of my findings. I traced the issue I had with an ajax script error. Since I found little information on the web on a fix for it, I wanted to make this post. The Issue: I have an ajax call to a script that returns contact information stored in a database. The xml result looks like this

    <rs>
    <coname>A Company</coname>
    <addr>123 main st.</addr>
    <city></city>
    <state></state>
    <zip>77001</zip>
    </rs>

    When the returned information include fields that are empty ie will throw an error "object required" when you try to access the value like... document.getElementById("city").value = xmlDoc.getElementsByTagName("city")[0].childNodes[0].nodeValue; ...this is the source of the ie error, firefox does not generate an error. Testing for empty string or null has no effect. To work around it you can test for an empty sring in the result from the database query and either use a character string to test for in your callback function or leave the value out of the xml and test for undefined. Like..

    <rs>
    <coname>A Company</coname>
    <addr>123 main st.</addr>
    <city>-1</city>
    <state>-1</state>
    <zip>77001</zip>
    </rs>

    or

    <rs>
    <coname>A Company</coname>
    <addr>123 main st.</addr>
    <zip>77001</zip>
    </rs>

    Anyone else have ideas on this issue, I would like to hear them. All the Best!

    D 1 Reply Last reply
    0
    • C cjoki

      Hello all, This is more of a posting of my findings. I traced the issue I had with an ajax script error. Since I found little information on the web on a fix for it, I wanted to make this post. The Issue: I have an ajax call to a script that returns contact information stored in a database. The xml result looks like this

      <rs>
      <coname>A Company</coname>
      <addr>123 main st.</addr>
      <city></city>
      <state></state>
      <zip>77001</zip>
      </rs>

      When the returned information include fields that are empty ie will throw an error "object required" when you try to access the value like... document.getElementById("city").value = xmlDoc.getElementsByTagName("city")[0].childNodes[0].nodeValue; ...this is the source of the ie error, firefox does not generate an error. Testing for empty string or null has no effect. To work around it you can test for an empty sring in the result from the database query and either use a character string to test for in your callback function or leave the value out of the xml and test for undefined. Like..

      <rs>
      <coname>A Company</coname>
      <addr>123 main st.</addr>
      <city>-1</city>
      <state>-1</state>
      <zip>77001</zip>
      </rs>

      or

      <rs>
      <coname>A Company</coname>
      <addr>123 main st.</addr>
      <zip>77001</zip>
      </rs>

      Anyone else have ideas on this issue, I would like to hear them. All the Best!

      D Offline
      D Offline
      daveyerwin
      wrote on last edited by
      #2

      if(xmlDocument.getElementsByTagName("city")){
      if(xmlDocument.getElementsByTagName("city").childNodes){
      alert(xmlDocument.getElementsByTagName("city")[0].nodeValue)
      }}

      That's just a sorta, the idea is to test for the object before accessing its property.

      modified on Thursday, April 1, 2010 7:27 PM

      C 1 Reply Last reply
      0
      • D daveyerwin

        if(xmlDocument.getElementsByTagName("city")){
        if(xmlDocument.getElementsByTagName("city").childNodes){
        alert(xmlDocument.getElementsByTagName("city")[0].nodeValue)
        }}

        That's just a sorta, the idea is to test for the object before accessing its property.

        modified on Thursday, April 1, 2010 7:27 PM

        C Offline
        C Offline
        cjoki
        wrote on last edited by
        #3

        the check I was using...

        if(typeof xmlDoc.getElementsByTagName("city")[0].childNodes[0] != "undefined")
        {
        document.getElementById("city").value = xmlDoc.getElementsByTagName("city")[0].childNodes[0].nodeValue;
        }

        IE did not complain about the object (childNode[0]) being undefined. Nor did FF. FF just assigned a blank string to city and IE would choke at the assignment and toss the error. Maybe I could have done this to check if the value is set, but I have not checked if it is even valid.

        if(typeof xmlDoc.getElementsByTagName("city")[0].childNodes[0].nodeValue != "undefined")

        I now use this and it works fine...

        if(typeof xmlDoc.getElementsByTagName("city")[0].childNodes[0] != "undefined")
        {
        if(xmlDoc.getElementsByTagName("city")[0].childNodes[0].nodeValue!="-1")
        {
        document.getElementById("city").value = xmlDoc.getElementsByTagName("city")[0].childNodes[0].nodeValue;
        }
        else
        {
        document.getElementById("city").value = "";
        }
        }

        I check server side for an empty string and instead return a -1 when one is found.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups