【问题标题】:Ajax ResponseText is getting true but can't write to divAjax ResponseText 越来越真实,但无法写入 div
【发布时间】:2026-01-18 11:35:01
【问题描述】:

Ajax 代码:

    jQuery(document).ready( function($) {
    var valueCheck;
    $('#acf-field_5cdc07b87c8f8-field_5cdc08405814f').on( 'change', function () {
         valueSelect = $(this).val();
         if ( parseInt ( valueSelect ) > 0 ) {
        $.ajax( ajaxurl, {
            type: 'POST',
            url: '<?php echo admin_url('admin-ajax.php'); ?>',
            data: { action: 'hesaplama', // The name of the WP action
                    value:  valueSelect,       // the dataVAlues
            },
            dataType: 'json',
            success: function ( response ) {     // to develop in case of success
                             if ( response.success ) {
                                  sonucum = response.html;  // Here we get the results of the PHP remote function
                                  $('#xcvb').html( sonucum );
                             }
                             else {
                                  $('#xcvb').html( '<span>Bu #id: ' +  valueSelect + ' ye ait bir içerik bulunamadı.</span>' );
                             }
                        },
            error: function ( errorThrown ) {   // to develop in case of error
                             console.log( errorThrown ); 
                        }, 
        });
         }
    });
});

函数.PHP:

function hesaplayici(){
$id    = (int) $_POST['value'];
$sonucum =  the_field('sertifika_aciklamasi', $id);}

响应文本显示在控制台上,但无法写入我的 div (id: #xcvb) 任何人都可以帮助我解决这个问题吗?

https://up.uac.ist/images/2019/06/17/Screenshot_2.png https://up.uac.ist/images/2019/06/17/Screenshot_3.png

【问题讨论】:

  • ajax 成功后,您可以在网络中查看您得到的响应吗?

标签: javascript php ajax wordpress


【解决方案1】:

看来您这里有一些问题。这是将 Ajax 与 Wordpress 结合使用的正确方法。

$.ajax({
            type: 'POST',
            url: ajax_object.ajax_url,
            data: {
                action: 'hesaplama',
                value: valueSelect
            }, 
            error: function (data) {
                console.log(data);
            }, 
            success: function (data) {
               
               console.log(data);
               if ( data == '') {
                  $('#xcvb').html( '<span>Bu #id: ' +  valueSelect + ' ye ait bir içerik bulunamadı.</span>' );
               }
               else {
                  $('#xcvb').append( data );
               }

            }
    });

您应该尝试回应您的回复。

function hesaplayici(){

$id = $_POST['value'];

$sonucum =  the_field('sertifika_aciklamasi', $id);

echo $sonucum;

die();

}

add_action( 'wp_ajax_send_hesaplayici', 'hesaplayici' );
add_action( 'wp_ajax_nopriv_hesaplayici', 'hesaplayici' );

【讨论】:

  • 噢,谢谢你!动作:'hesaplama',==> add_action('wp_ajax_send_hesaplama','hesaplayici'); add_action('wp_ajax_nopriv_hesaplama','hesaplayici');我将追加更改为 html,因为它已添加到 div 新日志中。
【解决方案2】:

请这样尝试:

在 php 方面:

$return = ['myvarname' => 'your data here'];
echo json_encode($return);
exit(); 

在js端:

success: function ( response ) { 
$(#idofyourdiv).html(response.myvarname);
}

【讨论】:

  • 转到网络选项卡,然后选择生成 JSON 响应的 .php 文件。回复标签上有什么?
  • 你解决了吗?问题是什么?我很高兴我能提供帮助
  • 是的,我用@dubvader 回答来修复它。