Using jQuery to parse XML returned from PHP script (imgur.com API)

后端 未结 2 1438
灰色年华
灰色年华 2021-01-15 19:52

Here\'s my jQuery:

var docname =  $(\'#doc\').val();

function  parseXml(xml)
{
  $(xml).find(\"rsp\").each(function()
  {
    alert(\"success\");
  });
}

$         


        
相关标签:
2条回答
  • 2021-01-15 20:30
    var docname =  $('#doc').val();
    

    Exactly where is this in your code and when will it be evaluated?
    My guess is that it's executed either when the <script> tag has been parsed or you've wrapped it in a $(document).ready() handler. Either way it get's evaluated before the user has actually typed something into the input/text control and docname will therefore be '' or even null all the time. You want the script to fetch the value not until the user has pressed the submit button.
    Try it with

    $('#submit').click(function() {
      $.ajax({
        type: "GET",
        url: "img_upload.php",
        data: "doc=" + $('#doc').val(),
        dataType: "xml",
        success: parseXml
      });
      return false;
    });
    

    edit: Even better, make the data property an object and let jquery handle the escaping of the value.

    data: {doc: $('#doc').val()}
    
    0 讨论(0)
  • 2021-01-15 20:34

    It could be that you have not set the header in the php script - this should be your first line.

    header('Content-Type: text/xml');
    
    0 讨论(0)
提交回复
热议问题