1

I tried to use jquery to get an asp-generated xml file as per read xml by jquery , but I get status = parsererror err = TypeError: data is null. Entering the xml file into the browser generates a perfectly good xml file. I hope I don't have to do a wget...

xmlhttp = "http://10.1.10.154:1014/dtsearch_2.asp?cmd=pdfhits&DocId=44&Index=C%3a%5cstream%5cSupport%5cIDX%5fESTATE&HitCount=4&hits=185+1ac+1d5+1ff+&hc=323&req=knife"

$.ajax({
  url: xmlhttp,
  method: "POST",
  dataType: "xml",
  success: function(data) {
      var xdoc = $(data);    // Note that jQuery has already done the parsing for us
      alert("Getting tutorials");
      var tutorials = xdoc.find("loc");
      alert("Found: " + tutorials.length);
      tutorials.each(function() {
          alert("Tutoral author: " + $(this).attr("pg"));
      });
  },
  error: function(jxhr, status, err) {
      alert("Ajax error: status = " + status + ", err = " + err);
  }
});

I use a local IP address and port 1014, but I don't see why any of that would matter...

Thanks, Nick

1 Answer 1

2

Take out the var xdoc = $(data) and just reference data directly.

$.ajax({
  url: xmlhttp,
  method: "POST",
  dataType: "xml",
  success: function(data) {
      alert("Getting tutorials");
      var tutorials = data.find("loc");
      alert("Found: " + tutorials.length);
      tutorials.each(function() {
          alert("Tutoral author: " + $(this).attr("pg"));
      });
  },
  error: function(jxhr, status, err) {
      alert("Ajax error: status = " + status + ", err = " + err);
  }
});

edit

This falls under the Same Origin Policy. If you need it to be on a separate port you will want to look into using JSONP.

note

As of jQuery 1.5, jQuery can convert a dataType from what it received in the Content-Type header to what you require. For example, if you want a text response to be treated as XML, use "text xml" for the dataType. You can also make a JSONP request, have it received as text, and interpreted by jQuery as XML: "jsonp text xml." Similarly, a shorthand string such as "jsonp xml" will first attempt to convert from jsonp to xml, and, failing that, convert from jsonp to text, and then from text to xml.

$.ajax({
    url: xmlhttp,
    method: 'POST',
    dataType: 'jsonp xml',
    success: function(data) {
        alert('Getting tutorials');
        var tutorials = data.find('loc');
        alert('Found: ' + tutorials.length);
        tutorials.each(function() {
            alert('Tutorial author: ' + $(this).attr('pg'));
        });
    },
    error: function() {
        alert('Ajax error: status = ' + status + ', err = ' + err);
    }
});

reference http://api.jquery.com/jQuery.ajax/

Sign up to request clarification or add additional context in comments.

4 Comments

nah, data is null. When I firebug it, I get no response, but XML is XML Parsing Error: no element found Location: moz-nullprincipal:{1ab824d6-817a-4c37-8256-893e5da80577} Line Number 1, Column 1: ^
Can you post your XML? Trying to re-create but I cannot access the XML file.
Figured it out. It's the port. Can you not specify a port with ajax?
No, it falls under the developer.mozilla.org/en/Same_origin_policy_for_JavaScript, If you need to do it on a separate port you should look into JSONP, I updated my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.