从Ajax响应中获取XML数据

时间:2015-03-28 00:22:28

标签: php jquery ajax xml wordpress

我从Wordpress AJAX请求中获取以下XML响应,但无法提取数据。我认为这是因为它被解析为CDATA,但我不明白为什么。

<?xml version="1.0" encoding="UTF-8"?>
<wp_ajax>
   <response action="mz_mindbody_ajax_add_to_class_0">
      <object id="0" position="1">
     <response_data><![CDATA[error]]></response_data>
     <supplemental>
        <classID><![CDATA[2237]]></classID>
        <message><![CDATA[Error in add to class (2237)]]></message>
     </supplemental>
      </object>
   </response>
</wp_ajax>

这是处理AJAX调用的php:

<script type='text/javascript'>
/* <![CDATA[ */
var my_parameters = {"ajaxurl":"http:\/\/localhost:8888\/wp-admin\/admin-ajax.php"};
/* ]]> */
</script>
<?php

// Ajax Handler
add_action( 'wp_ajax_my_ajax_handler', 'my_ajax_handler' );
function my_ajax_handler() {
    // Get the Post ID from the URL
    $classID = $_REQUEST['classID'];

    // Instantiate WP_Ajax_Response
    $response = new WP_Ajax_Response;

    if( wp_verify_nonce( $_REQUEST['nonce'], 'nonce-name' . $classID )){
    //Do something here

    $response->add( array(
        'data'  => 'success',
        'supplemental' => array(
        'classID' => 'did it',
        'message' => 'Class ID goes here', // ideally want to show $classID
        ),
     ) );
    } else {
    // Build the response if an error occurred
    $response->add( array(
        'data'  => 'error',
        'supplemental' => array(
        'classID' => 'nothing to see here',
        'message' => 'Error in add to class',
        ),
    ) );
    }
    // Whatever the outcome, send the Response back
    $response->send();

    // Always exit when doing Ajax
    exit();
}
//End Ajax

这是jQuery代码:

(function($){
$(document).ready(function($) {
    $( '.my_class' ).click( function( e ) {
        var link = this;
        var id   = $( link ).attr( 'data-id' );
        var nonce = $( link ).attr( 'data-nonce' );

        // This is what we are sending the server
        var data = {
            action: 'my_function',
            classID: id,
            nonce: nonce
        }
        // Change text of link
        $( link ).text( 'DOING IT' );

        // Post to the server
        $.post( my_parameters.ajaxurl, data, function( data ) {
            // Parse the XML response with jQuery
            // Get the Status
            console.log(data); //the XML data posted above
            var status = $( data ).find( 'response_data' ).text();
            // Get the Message
            var message = $( data ).find( 'supplemental message' ).text();
            // If we are successful, add the success message and remove the link
            console.log(status); // empty string
            if( status == 'success' ) {
            $( link ).parent().after( '<p><strong>' + message + '</strong></p>').remove();
            } else {
            // An error occurred, alert an error message
            alert( message );
            }
        });
        // Prevent the default behavior for the link
        e.preventDefault();
        });
    }); 
})(jQuery);

在教程中我跟随$response->add()次调用是在函数内。事实上我的不是导致问题的原因吗?

等等 - 我歪曲了data&#34;对象&#34;,这可能是问题的一部分(或全部)。整个数据&#34;对象&#34;实际上是字符串,在控制台中看起来像:

<script type='text/javascript'>
/* <![CDATA[ */
var mz_mbo_params = {"ajaxurl":"http:\/\/localhost:8888\/wp-admin\/admin-ajax.php"};
/* ]]> */
</script>
<?xml version='1.0' encoding='UTF-8' standalone='yes'?><wp_ajax><response action='mz_mindbody_ajax_add_to_class_0'><object id='0' position='1'><response_data><![CDATA[error]]></response_data><supplemental><classID><![CDATA[nothing to see here]]></classID><message><![CDATA[Error in add to class]]></message></supplemental></object></response></wp_ajax>

2 个答案:

答案 0 :(得分:1)

更新

给予回复data string;请注意,不确定string

返回的实际原始data的引用
var data = "<script type='text/javascript'>/* <![CDATA[ */var mz_mbo_params = {\"ajaxurl\":\"http:\/\/localhost:8888\/wp-admin\/admin-ajax.php\"};/* ]]> */</script><?xml version='1.0' encoding='UTF-8' standalone='yes'?><wp_ajax><response action='mz_mindbody_ajax_add_to_class_0'><object id='0' position='1'><response_data><![CDATA[error]]></response_data><supplemental<classID><![CDATA[nothing to see here]]></classID><message><![CDATA[Error in add to class]]></message></supplemental></object></response></wp_ajax>";

尝试使用jQuery.parseHTML()

var xml = $.parseHTML(data, document, false)[1]; // remove `script` element
// do stuff
$(xml).find("message");

尝试将$.parseXML()data参数一起使用,使用已解析documentElement xml参数的document调用jQuery()

// response `data` from `$.post()` callback
var xmlDocument = $.parseXML(data); 
// `documentElememt` `<wp_ajax></wp_ajax>` of response `xml` `data`
var xml = $(xmlDocument.documentElement); 
var status = xml.find("response_data").text();
var message = xml.find("supplemental message").text();
console.log(status); // empty string
if ( status == "success" ) {
   $( link )
   .parent()
   .after("<p><strong>" + message + "</strong></p>").remove();
} else {
   // An error occurred, alert an error message
   alert( message );
}

var data = '<wp_ajax><response action="mz_mindbody_ajax_add_to_class_0"><object id="0" position="1"><response_data><![CDATA[error]]></response_data><supplemental><classID><![CDATA[2237]]></classID><message><![CDATA[Error in add to class (2237)]]></message></supplemental></object></response></wp_ajax>'
var xmlDocument = $.parseXML(data);
var xml = $(xmlDocument.documentElement);
var status = xml.find("response_data").text();
var message = xml.find("supplemental message").text();
console.log(status); // empty string
if ( status == "success" ) {
   $( link )
   .parent()
   .after("<p><strong>" + message + "</strong></p>").remove();
} else {
   // An error occurred, alert an error message
   alert( message );
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

答案 1 :(得分:1)

这是您在wordpress配置中遇到的问题。使用ajax脚本标记修复导致预先挂起的XML-RPC响应的配置。它打破了反应。

您很可能已经在某些钩子上注册过于通用或者您的钩子函数没有过滤掉AJAX调用。