使用 jQuery ajax 响应数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3031062/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 14:45:34  来源:igfitidea点击:

Using jQuery ajax response data

jquery

提问by Theopile

I am using ajax post and am receiving data in the form of html. I need to split up the data and place pieces of the data all over the page. I built my response data to be something like <p id='greeting'> Hello there and Welcome </p> <p id='something'>First timer visiting our site eh'</p>It is a little more complicated and dynamic but I can figure it out if get this question answered. Thanks

我正在使用 ajax post 并以 html 的形式接收数据。我需要拆分数据并将数据片段放置在整个页面上。我将我的响应数据构建为类似<p id='greeting'> Hello there and Welcome </p> <p id='something'>First timer visiting our site eh'</p>它有点复杂和动态,但如果回答这个问题我可以弄清楚。谢谢

$.ajax({
            type:'POST',
            url: 'confirm.php',
            data: "really=yes&sure=yes",
            success:function(data){
                    //Need to split data here
            }
        });

回答by meder omuraliev

Update: Just realized you should probably do this:

更新:刚刚意识到你应该这样做:

success:function(data) {
    data = $('<div/>').append(data);
    $('#greeting',data).appendTo('#one')
    $('#something',data).appendTo('#two')
}

As you cant use .findproperly since it isnt a child but if you append it to an empty node you can. The other alternative would be using .filter

因为你不能.find正确使用,因为它不是一个孩子,但如果你将它附加到一个空节点,你可以。另一种选择是使用.filter

$.ajax({
            type:'POST',
            url: 'confirm.php',
            data: "really=yes&sure=yes",
            success:function(data){
                    $('#greeting',data).appendTo('#one')
                    $('#something',data).appendTo('#two')
            }
        });

You can extract from dataand append where you want to. You can also do something like return JSON instead, and instead of extracting html from html just access the html from an object.

您可以从中提取data并附加到您想要的位置。您还可以执行类似返回 JSON 的操作,而不是从 html 中提取 html,只需从对象访问 html。

$(data.greeting).appendTo('#one')
$(data.something).appendTo('#two')

The response would have to be like:

响应必须是这样的:

({ 'greeting':'html', 'something' :'other html' })

回答by Paul McMahon

Why don't you string the answers together within confirm.phpwith the |character, then when the string gets returned as the variable data you can split it with datas = data.split("|")and access the individual answers with data[0], data[1], etc.

你为什么不串在一起内的答案confirm.php|字符,那么当字符串被返回的变量数据,你可以把它分解datas = data.split("|")并获得与个人的答案data[0]data[1]等等。

回答by Computerish

(Meder's response will work if you are comfortable with JSON, but regular expressions are probably a little easier and will work just as well for this.)

(如果您对 JSON 感到满意,Meder 的响应将起作用,但正则表达式可能更容易一些,并且同样适用于此。)

You will probably need to break up the response text using regular expressions. For example, if the response text is:

您可能需要使用正则表达式分解响应文本。例如,如果响应文本是:

<p id='greeting'> Hello there and Welcome </p>
<p id='something'>First timer visiting our site eh'</p>

Then you could use some JavaScript like this:

然后你可以像这样使用一些 JavaScript:

var greeting = response_text.match(/<p id='greeting'>.*</p>/);
var something = response_text.match(/<p id='something'>.*</p>);

(This site is great for learning about regular expressions: http://gskinner.com/RegExr/)

(这个站点非常适合学习正则表达式:http: //gskinner.com/RegExr/