javascript - 如何检查我的响应是字符串还是 JSON 对象?

标签 javascript jquery json

我希望我的响应在 JSON 对象中。但在某些函数中它以字符串形式出现,我想解析它。我怎样才能做到这一点?这是我的职责。

function serverRequest(requestDATA, successCallback, errorCallback, hideIndicator, showAlert){
    var METHOD = "POST";
    var serverURL = 'http://istudy.com.pk/api/server.php'; //Path to Server.php
    //var serverURL = 'http://localhost/istudy/server.php'; //Path to Server.php
    var DATA_TYPE = 'json';
    var TIMEOUT = 20000;
    console.log(requestDATA);
    $$.ajax({
        url: serverURL,
        data: requestDATA,
        dataType: DATA_TYPE,
        type: METHOD,
        timeout: TIMEOUT,
        success: function(data){
            successCallback(data, hideIndicator, showAlert);
        },
        error: function(a, b, c){
            errorCallback(a, b, c, hideIndicator, showAlert);
        }
    }); 
}

这是我的回复文本

responseText: "{"status":"success","message":"Your Order has Processed. Please Note this pin <b style=\"background-color:green;\">osIyKvAp<\/b> and send it to the admin with payment details."}"

这是我的 PHP 函数

function confirm_order($id, $newarray, $cn) {
    $today = date('y-m-d');
    $expiry = date('Y-m-d', strtotime("+30 days"));
    $total_items = 0;
    $total_price = 0;
    foreach ($newarray as $key => $val) {
        $total_items++;
        foreach ($val as $doc) {
            $total_price = $total_price + $doc->doc_price;
        }
    }

    $pin = random_string('alnum', 8);

    $sql = "INSERT INTO orders (order_id, order_UserId, order_total_items, order_amount, date_created,order_status,order_pin)
    VALUES ('', '" . $id . "', '" . $total_items . "', '" . $total_price . "', '" . $today . "','0','" . $pin . "')";

    if (mysqli_query($cn, $sql)) {
        $order_id = mysqli_insert_id($cn);

        foreach ($newarray as $key => $val) {
            $total_docs = 0;
            $total_items++;
            $sep = '|';
            $sub_price = 0;
            $doc_list = '';
            foreach ($val as $doc) {
                $doc_list .= $doc->doc_id . $sep;
                $sub_price = $sub_price + $doc->doc_price;
                $total_docs++;
                $sep = '|';
            }

            $sql = "INSERT INTO cart_items (uc_id, uc_user_id, uc_course_id, uc_course_docs_id, uc_course_docs_num,uc_price,uc_order_num,uc_order_status,uc_dateBuy,uc_dateExpire)
            VALUES ('', '" . $id . "', '" . $key . "', '" . $doc_list . "', '" . $total_docs . "','" . $sub_price . "','" . $order_id . "','0','" . $today . "','" . $expiry . "')";

            mysqli_query($cn, $sql);
        }

        $response_array['status'] = 'success';
        $response_array['message'] = 'Your Order has Processed. Please Note this pin <b style="background-color:green;">' . $pin . '</b> and send it to the admin with payment details.';
    } else {
        $response_array['status'] = 'error';
        $response_array['message'] = 'An error occurred. Please try again later.';
    }
    echo json_encode($response_array);
}

当且仅当我的响应是字符串时,我可以对 JSON.parse 进行任何检查吗?

最佳答案

将其添加到您的成功处理程序中。

data = typeof data === 'string' ? data : JSON.parse(data);

关于javascript - 如何检查我的响应是字符串还是 JSON 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40773922/

相关文章:

javascript - 将 Google map JavaScript 库与 cordova 结合使用

jquery - 如何使用 jQuery 从 JSON 文件获取 key

ios - 在 iOS 上使用 Mantle 将 Json 数组转换为 Obj 属性

javascript - 如何在 json 数组中实现搜索

javascript - Node 表示没有数据到达服务器

javascript - 如何在 JavaScript 的 for 循环中分配 onclick ?

jquery - Bootstrap 轮播冲突 flexslider

javascript - 更改位于另一个页面中的另一个 div 的 CSS 属性

python - 使用 python 将 Dialogflow 代理导出/转换为 csv 或 excel 文件

javascript - Angular2 不渲染 ChildComponent 选择器