简体   繁体   English

发布数组缺少来自ajax请求的json

[英]Post array is missing json from ajax request

I'm trying to make a dynamic document previewer. 我正在尝试制作动态文档预览器。 The user can input certain document header options and based on their account information it will generate a document with a dynamic header. 用户可以输入某些文档标题选项,并根据其帐户信息将生成带有动态标题的文档。 I eventually want to preview a full pdf but I am working on just the header right now. 我最终想预览一个完整的pdf,但是我现在只在处理标题。

What I am trying to do is make a page with a form that the user can fill out, then press a button to preview the header. 我想做的是制作一个页面,用户可以填写表格,然后按一个按钮预览标题。

$.ajaxSetup({
type:       'POST',
timeout:    10000
});

$("#preview_header").click(function(){
    var full_url = //appropriate URL 

    var preview_data = {    
        "wsid":             "default",
        "page":             "default",
        "banner_area1":     "default",
        "banner_area2":     "default",
        "banner_area3":     "default",
        "uid":              "default",
        "fid":              "default",
        "cid":              "default",
        "assignment_type":  "default"
    };

    preview_data.wsid               = $("#worksheet_picker").val();
    preview_data.page               = $("#page_picker").val();
    preview_data.banner_area1       = $("#banner_area1").val();
    preview_data.banner_area2       = $("#banner_area2").val();
    preview_data.banner_area3       = $("#banner_area3").val();
    preview_data.uid                = $("#member_uid").val();
    preview_data.fid                = $("#family_id").val();
    preview_data.assignment_type    = 'family';
    preview_data.cid                = $("#class_id").val();

    var JSONText = JSON.stringify( preview_data );
    alert('Full JSON - ' + JSONText);

    $.ajax({
        async: true,
        url: full_url,
        data: { previewInfo : JSONText }, //Passes necessary form information
        dataType: 'json',
        success: function(output){
            var reply = output;
            if ( reply.status == "success" ){
                $("#preview").attr("src", reply.image );
            } else {
                alert('Failed to create image preview of the assignment.');
            } 
        } 
    });
});

As far as I can tell, the above method is working fine. 据我所知,上述方法运行良好。 It hits the right Codeigniter page and when the ajax method is set to return a hard coded image it works just fine. 它会到达正确的Codeigniter页面,并且当ajax方法设置为返回硬编码图像时,它就可以正常工作。 The AJAX seems to be well formatted but just in case here is what it outputs when I fill out forms with the corresponding values: AJAX似乎格式正确,但以防万一,当我用相应的值填写表格时,它会输出以下内容:

Full JSON - {"wsid":"4","page":"1","banner_area1":"link1",
"banner_area2":"link2","banner_area3":"link3",
"uid":"1","fid":"1","assignment_type":"family"}

So first off, let's start with what is working in the corresponding controller method for the ajax reply: 所以首先,让我们从ajax答复的相应控制器方法中的工作开始:

$data = array(
'status'    => 'success',
'image'     => //static image link
);

$this->output->set_content_type('text/javascript;charset=UTF-8');
echo json_encode($data);

But whenever I try to modify it like so: 但是每当我尝试像这样修改它时:

$preview_data = json_decode($this->input->post('previewInfo'));

//Got this one
mail('me@gmail.com', 'Start Email', 'Some email' ); 
//Empty email
mail('me@gmail.com', 'Dump Post', var_dump($_POST)); 
//Empty email
mail('me@gmail.com', 'Post data', var_dump($preview_data) );
//returns an email with 1 for body
mail('me@gmail.com', 'Post data', print_r($this->input->post()) ); 
//returns an email with 1 for body
mail('me@gmail.com', 'Post data', 
     print_r($this->input->post('previewInfo')) ); 
//returns an email with 1 for body
mail('me@gmail.com', 'Post data', print_r($preview_data) );

$data = array(
'status'    => 'success',
'image'     => //static image link
);

$this->output->set_content_type('text/javascript;charset=UTF-8');
echo json_encode($data);

The modified one doesn't return the static data either. 修改后的代码也不返回静态数据。 So it would seem that the post array is not being intialized properly. 因此,似乎未正确初始化post数组。 Anyone see the bug? 有人看到错误了吗?

If you want to send your Ajax request with the post method, you need to set type to POST in your $.ajax options. 如果要使用post方法发送Ajax请求,则需要在$.ajax选项中将type设置为POST

Also, async is true by default, so setting it is not necessary. 另外,默认情况下async为true,因此无需设置。 Additionally, you should consider using .done and .fail as opposed to success and fail , as they are slated to be deprecated very soon. 另外,您应该考虑使用.done.fail而不是successfail ,因为它们很快就会被弃用。

Like so: 像这样:

$.ajax({
    type: "POST",
    url: full_url,
    data: { previewInfo : JSONText }, //Passes necessary form information
    dataType: 'json'
}).done(function (output, textStatus, jqXHR) {
    var reply = output;
    if ( reply.status == "success" ){
        $("#preview").attr("src", reply.image );
    } else {
        alert('Failed to create image preview of the assignment.');
    } 
}).fail(function (jqXHR, textStatus, errorThrown) {
    // now you have the XHR, text status, and error to debug with
});

There was 2 problems that lead to the solution to this problem: 有两个问题可以解决这个问题:

1) 1)

As Austin and Gavin said: var_dump and print_r should not be outputting to the browser. 正如Austin和Gavin所说:var_dump和print_r不应输出到浏览器。 The solution was to debug with firefox extensions / chrome. 解决方案是使用firefox扩展/ chrome进行调试。

2) 2)

$preview_data = json_decode($this->input->post('previewInfo'));

was changed to 改为

$preview_data = json_decode($this->input->post('previewInfo'), true);

The secondy optional parameter of json_decode is to tell the method whether you are expecting an associative object or a list of things. json_decode的第二个可选参数是告诉方法您是要关联对象还是事物列表。 As it happens, I was reading in and wanted an associative array. 碰巧的是,我正在阅读并想要一个关联数组。

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

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