简体   繁体   English

如何格式化此Ajax响应数据?

[英]How to format this Ajax Response Data?

I am new to ajax and managed to get cross domain ajax response data using a proxy. 我是ajax的新手,并设法使用代理获取跨域ajax响应数据。 Here is the output 这是输出

  {"error":"\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n \r\n\t\t
   You entered invalid Roll-Number, Please enter valid Roll Number.
  \r\n"}

How do I format it to get rid of everything except "You entered invalid Roll-Number, Please enter valid Roll Number" 我如何格式化它以消除除“您输入的无效卷号,请输入有效的卷号”以外的所有内容

EDIT: Is there some valid json function to format it or I do it through javascript? 编辑:是否有一些有效的json函数对其进行格式化,或者我通过JavaScript进行了格式化?

SOLUTION FOUND: 找到的解决方案:

Since it is valid JSON data I could use eval function to process it: 由于它是有效的JSON数据,因此我可以使用eval函数来处理它:

eval("var jsonDataFormatted =   ("+XMLHttpRequestObject.responseText+")");
console.log(jsonDataFormatted.error);

The problem should be solved with javascript 该问题应使用javascript解决

Let's say you have your data in a variable called data 假设您将数据存储在名为data的变量中

data = {"error":"\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n \r\n\t\t You entered invalid Roll-Number, Please enter valid Roll Number.\r\n"}

All you need to do is to trim the string so that you can remove spaces in the begining or in the end 您所需要做的就是修剪字符串,以便可以删除开头或结尾的空格

data.error = data.error.trim();

Update 更新资料

If the solution you have found is eval then you better avoid it, this solution may help you 如果你已经找到了解决办法是eval那么你最好避开它,该解决方案可以帮助你

var jsonDataFormatted = JSON.parse(XMLHttpRequestObject.responseText);
console.log(jsonDataFormatted.error);

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

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