【问题标题】:How to parse json response using JavaScript?如何使用 JavaScript 解析 json 响应?
【发布时间】:2016-03-12 09:28:55
【问题描述】:

我是电话间隙应用程序开发的新手,并且 我正在尝试从我的应用程序调用 Web 服务以从服务器获取数据。 我成功调用了 REST Web 服务,但在使用 JavaScript 解析 JSON 响应时遇到问题。

我调用服务的代码如下:

     $.ajax({
                    type: "POST",
                    url: "http://www.url.php",
                    contentType: "application/x-www-form-urlencoded",
                    data: dataString,
                    success: function(response) {
                        alert("success!");

                    },
                    error: function(request, status, error) {
                    console.log("Error status " + status);
                    console.log("Error request status text: " + request.statusText);
                    console.log("Error request status: " + request.status);
                    console.log("Error request response text: " + request.responseText);
                    console.log("Error response header: " + request.getAllResponseHeaders());
                    }
            });

我能够进入代码的成功块,但问题是我无法使用 JavaScript 解析响应。 请指导或帮助我完成任务。

谢谢。

【问题讨论】:

  • 您是否尝试使用JSON.parse 来解析共振?

标签: javascript android json cordova


【解决方案1】:
success: function(response) {
   var resp = JSON.parse(response);
   alert(resp.text); // Login success or Fail
}

【讨论】:

  • 虽然这段代码 sn-p 可以解决问题,但including an explanation 确实有助于提高帖子的质量。请记住,您是在为将来的读者回答问题,而这些人可能不知道您提出代码建议的原因。
【解决方案2】:

我按如下方式完成:

     $.ajax({
                    type: "POST",
                    url: "http://www.url.php",
                    contentType: "application/x-www-form-urlencoded",
                    data: dataString,
                    success: function(response) {

                    //entered in the success block means our service call is succeeded properly

                        var resp = JSON.stringify(response.text); // we are accessing the text from the json object(response) and then converting it in to the string format 
                        console.log(JSON.stringify(response)); // print the response in console
                        alert(resp); // alert the response

                    },
                    error: function(request, status, error) {
                    console.log("Error status " + status);
                    console.log("Error request status text: " + request.statusText);
                    console.log("Error request status: " + request.status);
                    console.log("Error request response text: " + request.responseText);
                    console.log("Error response header: " + request.getAllResponseHeaders());
                    }
            }); 

你可以参考json对象:{"text":"Login success","status":1,"school_detail_id":"72","data":[{"section_id":"541 ","class_name":"1-A"},{"section_id":"542","class_name":"2-A"}]}

因此警报将显示登录成功/失败。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多