开发者

Problem with jquery retrieving xml from asp

开发者 https://www.devze.com 2023-02-21 21:12 出处:网络
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 perfe

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...

Thank开发者_如何学Pythons, Nick


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/

0

精彩评论

暂无评论...
验证码 换一张
取 消