简体   繁体   English

将ajax对象转换为字符串

[英]Converting an ajax object into a string

I have an ajaxed in element that I'm trying to convert into a string that can be stored into a variable.我在元素中有一个ajaxed,我试图将其转换为可以存储到变量中的字符串。 After I put it into the variable, the variable does not console.log ;我把它放入变量后,变量没有console.log it says it's an object.它说它是一个对象。 How do I get it to print out the content of the variable?我如何让它打印出变量的内容?

Here's the code I'm using:这是我正在使用的代码:

$.get('/Default.aspx?PageID=15018909&A=WebApp&CCID=21437&Page=1&Items=500', function(data){
    $('#ajaxResults').html(data).find("ul").remove(); 
    var ajaxResult = $('#ajaxResults').html($('#myArray').text());
    console.log(ajaxResult);
});

I also need to run a replace() on that ajax result, but I can't because the object isn't a string.我还需要在该 ajax 结果上运行 replace(),但我不能,因为该对象不是字符串。

You are taking the data back from $.get() and sending it to an element, then reading the element in. data already contains the information you need, see $.get您正在从$.get()取回data并将其发送到一个元素,然后读取该元素。 data已经包含您需要的信息,请参阅$.get

Why wouldn't you just retrieve the unparsed text data from the jqXHR parameter to the success callback?为什么不从jqXHR参数中检索未解析的文本数据到成功回调?

$.get('/Default.aspx?PageID=15018909&A=WebApp&CCID=21437&Page=1&Items=500', function(data,status,jqXHR){
    console.log(jqXHR.responseText);
});

Or conversely, just look at the data itself?或者相反,只看数据本身?

$.get('/Default.aspx?PageID=15018909&A=WebApp&CCID=21437&Page=1&Items=500', function(data,status,jqXHR){
    console.log(data);
});

If you know the data is already an object如果你知道数据已经是一个对象

JSON.stringify(data)

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

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