javascript - jQuery how to read this JSON string from responseText - Stack Overflow

I have tried almost everyway here on stack overflow, but somehow I cannot read JSON string data returne

I have tried almost everyway here on stack overflow, but somehow I cannot read JSON string data returned from a success function call in jquery ajax. my success function receives following JSON string:

Object {
  readyState = 4, responseText = "{"
  Response ":200,"
  Data ":"
  6 ","
  Message ":"
  6 "}", status = 200, statusText: "OK"
}

This is my success callback:

success: function(response, msg, responseText) {
   if (response.Response == 200) {
     console.log("Data was submitted");
     var obj = responseText;

     console.log(typeof obj);
   } else if (response.Response == 400) {
     console.log("there was some error");
   }
 }

When success function is fired and checks for status code, it executes console.log("Data was submitted"); statement successfully, however, I am not able to access the "Data":"6" key/value pair.

So far I have tried doing this:

var obj = responseText;
console.log(obj.Data);

and

console.log(obj.data[1]);

and numerous other ways, but either it says "undefined" or gives and error. However, when I console.log(obj), in console it shows 'obj'. which means I am getting a JSON object.

Please note that I have also tried:

obj = jQuery.parseJSON(responseText);

which gives me an error: SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data

What to do in this situation? I want to be able to extract the value of a key name "Data": and and assign its value ="6" to a variable.

I have tried almost everyway here on stack overflow, but somehow I cannot read JSON string data returned from a success function call in jquery ajax. my success function receives following JSON string:

Object {
  readyState = 4, responseText = "{"
  Response ":200,"
  Data ":"
  6 ","
  Message ":"
  6 "}", status = 200, statusText: "OK"
}

This is my success callback:

success: function(response, msg, responseText) {
   if (response.Response == 200) {
     console.log("Data was submitted");
     var obj = responseText;

     console.log(typeof obj);
   } else if (response.Response == 400) {
     console.log("there was some error");
   }
 }

When success function is fired and checks for status code, it executes console.log("Data was submitted"); statement successfully, however, I am not able to access the "Data":"6" key/value pair.

So far I have tried doing this:

var obj = responseText;
console.log(obj.Data);

and

console.log(obj.data[1]);

and numerous other ways, but either it says "undefined" or gives and error. However, when I console.log(obj), in console it shows 'obj'. which means I am getting a JSON object.

Please note that I have also tried:

obj = jQuery.parseJSON(responseText);

which gives me an error: SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data

What to do in this situation? I want to be able to extract the value of a key name "Data": and and assign its value ="6" to a variable.

Share Improve this question edited Mar 3, 2017 at 15:02 ROMANIA_engineer 56.8k30 gold badges210 silver badges205 bronze badges asked Dec 23, 2015 at 15:20 ZafarZafar 3215 silver badges17 bronze badges 5
  • response.Response, response.Data and response.Message – Rory McCrossan Commented Dec 23, 2015 at 15:21
  • Hi @RoryMcCrossan Thank you for your response. I tried doing this as you suggested: obj = responseText; console.log(obj.Data); However, it suggests: undefined – Zafar Commented Dec 23, 2015 at 15:23
  • Hi @RoryMcCrossan Oh Oh Oh! IT WORKED. Thank you man. Such a simple mistake. Please post it as an answer. So that I can mark it. – Zafar Commented Dec 23, 2015 at 15:25
  • response.Response is returning the correct value. In the JSON object you provided Response and Data are part of the same object... Have you tried response.Data instead of responseText.Data – Antonio Manente Commented Dec 23, 2015 at 15:25
  • @Zafar Darin got the same answer. The issue is that you're accessing the wrong parameter of the success handler – Rory McCrossan Commented Dec 23, 2015 at 15:26
Add a ment  | 

2 Answers 2

Reset to default 4

The first parameter of the success callback is what you need, not the third. The first parameter will represent the body of the response as returned from the server. Also you don't need to be checking for anything different than 200 status code in a success callback. That's what the error callback is designed for because the success callback will never be fired if your server returns 400 status code.

So:

dataType: 'json',
success: function (response) {
    console.log("Data was submitted");
    console.log(response.Data);
},
error: function() {
    console.log("there was some error");
}

The success callback is success: function(data, textStatus, jqXHR )

So the 1st, data will contain the data returned to the success function.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744585263a4582272.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信