duannv2081 2019-06-17 15:27
浏览 116
已采纳

Ajax ResponseText正在变为true但无法写入div

Ajax Code:

    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 ); 
                        }, 
        });
         }
    });
});

Functions.PHP :

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

Responsetext show on console but can't write to my div ( id: #xcvb ) Any one can help me about this ?

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

  • 写回答

2条回答 默认 最新

  • dongtuan8547 2019-06-17 20:30
    关注

    Looks like you have a few things wrong here. First, I'm unsure how you are running jQuery with a PHP echo statement in it. You shouldn't be able to do that. This is the proper way to use Ajax with 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 );
                   }
    
                }
        });
    
    

    You should try echoing your response.

    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' );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?