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

时间:2022-12-08 11:10:20

I am new to ajax and managed to get cross domain ajax response data using a proxy. Here is the output

我是ajax的新手,并设法使用代理获取跨域ajax响应数据。这是输出

  {"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);

1 个解决方案

#1


0  

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 remove all the \r\n while you the string contains them, and then you trim the string so that you can remove spaces in the begining or in the end

您需要做的就是在字符串包含它们时删除所有\ r \ n,然后修剪字符串,以便您可以在开头或结尾删除空格

while(data.error.indexOf("\r\n")>=0) 
    data.error = data.error.replace("\r\n","");
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);

#1


0  

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 remove all the \r\n while you the string contains them, and then you trim the string so that you can remove spaces in the begining or in the end

您需要做的就是在字符串包含它们时删除所有\ r \ n,然后修剪字符串,以便您可以在开头或结尾删除空格

while(data.error.indexOf("\r\n")>=0) 
    data.error = data.error.replace("\r\n","");
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);