简体   繁体   English

使用WordPress将JSON对象发送到jQuery Tabs的AJAX

[英]Send a JSON object to AJAX of jQuery Tabs using WordPress

I am using jQuery Tabs to program a messaging system using WordPress. 我正在使用jQuery Tabs使用WordPress编写消息系统。 The point is that when the user clicks one tab, which represents one conversation, an AJAX call is performed automatically with the functionality of jQuery Tabs. 关键是,当用户单击一个代表一个对话的选项卡时,将使用jQuery Tabs功能自动执行AJAX调用。

I programmed the jQuery Tab to call an action (that I programmed as well). 我对jQuery Tab进行了编程以调用一个动作(我也对它进行了编程)。 The action is programmed in PHP using WordPress and I can call it through localhost/mywebpage/wp-admin/admin-ajax.php?action=my_action. 该动作是使用WordPress在PHP中编程的,我可以通过localhost / mywebpage / wp-admin / admin-ajax.php?action = my_action对其进行调用。

The problem is the following: The default functionality of the jQuery tabs expects the raw output of the PHP file (the ajax action). 问题如下:jQuery选项卡的默认功能需要PHP文件的原始输出(ajax操作)。 This means I have to code HTML on the PHP file so I just put the response on the tabs panel (ui.panel.html). 这意味着我必须在PHP文件上编码HTML,因此我只将响应放在选项卡面板(ui.panel.html)上。 However I think this is inefficient and I would like to create a JSON object using WordPres function wp_send_json_success( jsonObject ) which is received on the ui.ajaxSettings.dataFilter function. 但是我认为这效率低下,我想使用ui.ajaxSettings.dataFilter函数上接收到的WordPres函数wp_send_json_success(jsonObject)创建一个JSON对象。

When I send HTML to the ui.ajaxSettings.dataFilter function, everything is correctly displayed on the jQuery tabs panel. 当我将HTML发送到ui.ajaxSettings.dataFilter函数时,所有内容都会正确显示在jQuery选项卡面板上。 But when I send a json success, I can see it on the console but I can't display it on the jQuery tabs panel. 但是,当我发送成功的json时,可以在控制台上看到它,但不能在jQuery选项卡面板上显示它。 It appears for a millisecond and then disappears. 它出现一毫秒,然后消失。 So the JSON object is being received but for some reason can't be displayed. 因此正在接收JSON对象,但由于某些原因无法显示。 The only way something is displayed at the jQuery tabs panel is by sending raw HTML by my PHP function. 在jQuery选项卡面板上显示某些内容的唯一方法是通过PHP函数发送原始HTML。 Here is a very simple code example: 这是一个非常简单的代码示例:

This is the JS function: 这是JS函数:

$( selector ).tabs( {
    beforeLoad: function( event, ui ) {
        ui.ajaxSettings.dataFilter = function( response ) {
            console.log( response );
            ui.panel.html( response );
    }
} );

This is the PHP function that works good: 这是运行良好的PHP函数:

<?php
    echo 'Hello World!';

This is the PHP function that displays on the panel por a millisecond and then disappears. 这是PHP函数,在面板上显示一毫秒,然后消失。

<?php
    wp_send_json_success( 'Hello World! );

In my opinion, I think something else is executing and erasing what I have displayed on the ui.panel.html but I'm not that expert in jQuery tabs so if there is anyone out there with more experience that can tell me what it's going on I would really appreciate it. 以我的观点,我认为还有其他事情正在执行和擦除ui.panel.html上显示的内容,但是我不是jQuery选项卡上的专家,因此,如果有任何人有更多的经验可以告诉我这是怎么回事我真的很感激。

I got it working and the fix was obvious but I am still unsure if this is the best way to do it. 我可以正常工作,并且解决方法很明显,但是我仍然不确定这是否是最佳方法。 This is what I did: 这是我所做的:

var html = $( '<div>' ).addClass( 'wrapper' );

$( selector ).tabs( {
    beforeLoad: function( event, ui ) {
        ui.ajaxSettings.dataFilter = function( response ) {

            // Do whatever you want with the JSON object (obviously validations)
            // In my case I want to create HTML elements with info from the response
            var response_js_array = JSON.parse( response );

            html.append( $( '<div>' ).text( response_js_array['message'] ) );
    },
    load: function( event, ui ) {

        ui.panel.html( html );

        // Do also whatever you want after the messages have been loaded. In my case, 
        // I scroll down to the last message
        scrollDown(); // This is also a function I programmed
    }
} );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM