简体   繁体   English

将数据传递给ajax调用成功

[英]Passing data to ajax call on success

I wanted to pass the data from json_encode() function into jquery. 我想将数据从json_encode()函数传递到jquery中。 And on success I wanted to reload the specific div. 成功之后,我想重新加载特定的div。

Trigger: 触发:

<a id="js-delete-file" href="#" data-url="<?php echo site_url('document_items/remove/'. $value['id'].'/'. $value['filename']) ?>">Remove</a>

PHP: PHP:

public function remove ($id=null, $filename) {

    $dir = "_resources/docs/";
    if ($this->doc_item->remove($id)) {
        unlink($dir.$filename);
        $status = 'success';
        $msg = 'File successfully deleted';
    } else {
        $status = 'error';
        $msg = 'Something went wrong when deleting the file, please try again';
      }

      echo json_encode(array('status' => $status, 'msg' => $msg));
  }

JQUERY: JQUERY:

$('#js-delete-file').click(function(e){

    var url = $(this).attr('data-url');
    $.ajax({
      type: 'POST',
      url: url,
      success: function(result) {
        $('#uploaded-files').html(result);
      },
    });
  });

after I click the button it will give me this {"status":"success","msg":"File successfully deleted"} instead of the actual html. 单击按钮后,它会给我这个{"status":"success","msg":"File successfully deleted"}而不是实际的html。

Use param dataType for access to field status 使用param dataType访问字段状态

$.ajax({
      type: 'POST',
      url: url,
      dataType: "json",
      success: function(result) {
        if (result.status == 'success') {
          $('#uploaded-files').html(result.msg);
        }
      }
    });

Try this 尝试这个

$('#js-delete-file').click(function(e){

    var url = $(this).attr('data-url');
    $.ajax({
      type: 'POST',
      url: url,
      success: function(result) {
         var data = JSON.parse(result);
         $('#uploaded-files').html(data.msg);
      },
    });
});

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

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