简体   繁体   English

在div中的codeigniter成功后无法显示来自ajax的数据

[英]cant show the data from ajax after success in codeigniter in a div

after submitting ajax i am getting the data bask but in console only the data does not in a div in codeigniter here is the code 提交ajax后,我正在获取数据,但是在控制台中,只有数据不在codeigniter中的div中,这是代码

 jQuery('#npost').submit(function(e){

        e.preventDefault();
        var formData = new FormData(this);

        var url= '<?php echo base_url("user/postData"); ?>';

        jQuery.ajax({

            type: "POST",
            url:url,
            data: formData,
            dataType: 'json',
            cache: false,
            contentType: false,
            processData: false,
            success: function(data)
            {
              console.log(data);


        jQuery("div#output_post").show();
        jQuery("div#data").html(data);

      },
            error: function(data){
            //error function
          }
       });            
    });

now 现在

 $post = $this->input->post();
    print_r($post);
    unset($post['submit']);
    $this->load->model('Pmodel');
    $post_data=$this->Pmodel->post_data($post);

   echo json_encode($post);

but doing this only shows the value in console the success function not works 但是这样做只能显示控制台中的值,成功功能不起作用

If $post_data is running to the model, perhaps you should be returning the result from that? 如果$post_data正在运行到模型,也许您应该从中返回结果?

At the moment you are sending back the original data sent from your ajax call... you cannot be sure that $post_data=$this->Pmodel->post_data($post); 目前,您正在发回从ajax调用发送来的原始数据...您不确定$post_data=$this->Pmodel->post_data($post); is actually running... typically you would have this function return what you want to display, and send back back using json_encode($post_data) 实际上正在运行...通常,您将使此函数返回要显示的内容,然后使用json_encode($post_data)发回

jQuery("div#data").html(data); will probably only output correct html, as the data its getting back (from what I can see is probably a JSON object jQuery("div#data").text(data); should work... though I cannot see anything to do with your DOM as the view isn't being shown to us... 可能只会输出正确的html,因为返回的数据(从我看到的可能是JSON对象jQuery("div#data").text(data);应该起作用...尽管我看不到有任何事情要做与您的DOM,因为视图未显示给我们...

i think i found the solution for this i needed to get the json_encode data to read in the string form first in my ajax success. 我想我找到了解决这个问题的方法,这是我获得ajax成功后首先需要获取json_encode数据以字符串形式读取的方法。 so i did this 所以我做到了

success: function(data) { var ParsedObject = JSON.stringify(data); 成功:function(data){var ParsedObject = JSON.stringify(data);

         var json = $.parseJSON(ParsedObject);

         json.textdata;

         $('#result_table').append(json.textdata);

  }

thus getting the value of the textdata that i want to show now wherever i want to show it just append the data to that div. 这样就获得了我现在想要显示的textdata的值,只需将数据附加到该div即可。

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

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