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:
我几乎在这里尝试堆栈溢出,但不知怎的,我无法读取jquery ajax中成功函数调用返回的JSON字符串数据。我的success函数接收以下JSON字符串:
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.
当触发成功函数并检查状态代码时,它执行console.log(“数据已提交”);声明成功,但是,我无法访问“数据”:“6”键/值对。
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.
和许多其他方式,但要么它说“未定义”或给出和错误。但是,当我在console.log(obj)时,在控制台中显示'obj'。这意味着我正在获取一个JSON对象。
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
这给了我一个错误:SyntaxError:JSON.parse:JSON数据第1行第2列的意外字符
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.
在这种情况下该怎么办?我希望能够提取键名“Data”的值:并将其值=“6”赋给变量。
2 个解决方案
#1
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.
成功回调的第一个参数是您需要的,而不是第三个。第一个参数表示从服务器返回的响应主体。此外,您无需在成功回调中检查不同于200状态代码的任何内容。这就是错误回调的设计原因,因为如果您的服务器返回400状态代码,将永远不会触发成功回调。
So:
所以:
dataType: 'json',
success: function (response) {
console.log("Data was submitted");
console.log(response.Data);
},
error: function() {
console.log("there was some error");
}
#2
1
The success callback is success: function(data, textStatus, jqXHR )
成功回调是成功的:函数(data,textStatus,jqXHR)
So the 1st, data
will contain the data returned to the success
function.
所以第一,数据将包含返回到成功函数的数据。
#1
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.
成功回调的第一个参数是您需要的,而不是第三个。第一个参数表示从服务器返回的响应主体。此外,您无需在成功回调中检查不同于200状态代码的任何内容。这就是错误回调的设计原因,因为如果您的服务器返回400状态代码,将永远不会触发成功回调。
So:
所以:
dataType: 'json',
success: function (response) {
console.log("Data was submitted");
console.log(response.Data);
},
error: function() {
console.log("there was some error");
}
#2
1
The success callback is success: function(data, textStatus, jqXHR )
成功回调是成功的:函数(data,textStatus,jqXHR)
So the 1st, data
will contain the data returned to the success
function.
所以第一,数据将包含返回到成功函数的数据。