简体   繁体   English

Ajax响应数据未定义

[英]ajax response data is undefined

i have problem with ajax post submit here is my script 我对ajax发布有问题,这里是我的脚本

function postad(actionurl) {
        if (requestRunning) return false ;

        if (! $("#editadform").valid()) {
            validator.focusInvalid();
            return false ;
        }

        $('#ajxsave').show() ;
        requestRunning = true ;
        var postData = $('#editadform').serializeArray();
        $.ajax(
        {
            url : actionurl,
            type: "POST",
            data : postData,
            success:function(data, textStatus, jqXHR)
            {
                $('#diverrors').html(data.errors) ;
                $('#divalerts').html(data.alerts) ;

                if (data.status=='success') {  alert(data.status);
                  $('#siteid').val(data.siteid) ;
                  if ($('#adimager').val())
                    $('#divlmsg').html(data.alertimage) ;
                  $('#editadform').submit() ;

                } else {

                  $('#ajxsave').hide() ;
                }
            },
            error: function(jqXHR, textStatus, errorThrown)
            {
                $('#ajxsave').hide() ;
            },
            complete: function() {
                requestRunning = false;
            }
        });

        $('.btn').blur() // remove focus
        return false ;
    }

This works on if (textStatus=='success') { 这适用于if (textStatus=='success') {

but when the action fails, alert(data.status) shows Undefined. 但是当操作失败时, alert(data.status)显示为Undefined。

Using FireBug, I can see that the data is correctly returned. 使用FireBug,我可以看到数据已正确返回。 Why is data.status "Undefined" then? 为什么data.status为“未定义”?

If you don't specify the dataType field of an $.ajax() call in jQuery, it formats the response as plain text. 如果您未在jQuery中指定$.ajax()调用的dataType字段,则会将响应的格式设置为纯文本。 A workaround to your code would be to either include dataType: "JSON" into your $.ajax() parameters, or alternatively, in your success function, parse the plain text response as a JSON object, by using the folllowing: 代码的一种解决方法是将dataType: "JSON"包含在$.ajax()参数中,或者在success函数中,使用以下方法将纯文本响应解析为JSON对象:

data = JSON.parse(data); // You can now access the different fields of your JSON object

UPDATE: 更新:

yes i have not status field in action url, how to add data status field in php code? 是的,我在操作URL中没有status字段,如何在PHP代码中添加数据状态字段?

When creating your PHP script that is intended to return the JSON data, you first need to build an array and then encode it as JSON. 创建旨在返回JSON数据的PHP脚本时,您首先需要构建一个数组,然后将其编码为JSON。

So, suppose you have a PHP script that either succeeds and produces some data that you put into a $data variable, or fails, then the following style could be adopted: 因此,假设您有一个PHP脚本成功执行并生成了一些数据,这些数据被放入$data变量中,或者失败了,那么可以采用以下样式:

<?php
    // ^^ Do your PHP processing here
    if($success) { // Your PHP script was successful?
        $response = array("status" => "success!", "response" => $data);
        echo json_encode($response);
    }
    else {
        $reponse = array("status" => "fail", "message" => "something went wrong...");
        echo json_encode($response);
    }
?>

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

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