简体   繁体   English

你如何拆分AJAX响应,这样你就不必提出多个请求?

[英]How do you split up an AJAX response so you don't have to make several requests?

I couldn't think of a better title, but here is the explanation: 我想不出一个更好的头衔,但这里是解释:

I am making an ajax request that looks like this: 我正在制作一个看起来像这样的ajax请求:

...
xmlhttp.send("data1=" + $data);
...

document.getElementById("data1display").innerHTML= xmlhttp.responseText;
...

Now the page has developed and I want to send another variable, data2 to the same php page for processing. 现在页面已经开发了,我想将另一个变量data2发送到同一个php页面进行处理。

I realise I can add data to xmlhttp.send like "data1=" + $data + "data2=" + $data2 我意识到我可以将数据添加到xmlhttp.send,如"data1=" + $data + "data2=" + $data2

But! 但! How do I split the response to update two separate elements? 如何拆分响应以更新两个单独的元素? (data1display and data2display). (data1display和data2display)。

the code that builds the response: 构建响应的代码:

if ( isset( $_POST["data1"] ) ) 

 die($object->function()); //returns html to fill the specified element.

If your php processing page sends back JSON instead of html, you could expect the following: 如果你的php处理页面发送回JSON而不是html,你可以期待以下内容:

{
    "data1display": "<div>whatever</div>",
    "data2display": "<div>something</div>"
}

and then in your js success callback, you could do something like: 然后在你的js成功回调中,你可以做类似的事情:

var targetIdArray = ["data1display", "data2display"];
var responseJSON = JSON.parse(xmlhttp.responseText);
for (var i = 0; i < targetIdArray.length; i++) {
    document.getElementById(targetIdArray[i]).innerHTML= responseJSON[targetIdArray[i]];
}

If you want to stick with html in the response, you can add a class or ID to each part of the response and grab the correct nodes from the response using regular old jQuery selectors. 如果你想在响应中坚持使用html,你可以在响应的每个部分添加一个类或ID,并使用常规的旧jQuery选择器从响应中获取正确的节点。

Eventually, you may want to change your frontend framework though, so it may be even better to just pass back the data you want to present and let the client render it. 最后,您可能希望更改前端框架,因此最好只传回要呈现的数据并让客户端呈现它。 This would mean not passing html back to the client at all and instead using some templating solution like handlebars . 这意味着根本不会将html传递回客户端,而是使用一些模板解决方案,如把手

{
    "data1display": "whatever",
    "data2display": "something"
}

you could then make a couple of small templates like: 然后你可以制作一些小模板,如:

<div>{{ content }}</div>

and render them in the client. 并在客户端呈现它们。

Can you not putan if statement and say if this request is set then do 你不能putan if语句,并说如果这个请求设置然后做

document.getElementById("data1display").innerHTML= xmlhttp.responseText; document.getElementById(“data1display”)。innerHTML = xmlhttp.responseText;

Else do this 否则这样做

document.getElementById("data2").innerHTML= xmlhttp.responseText; document.getElementById(“data2”)。innerHTML = xmlhttp.responseText;

Just add if statements into your ajax 只需在您的ajax中添加if语句即可

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

相关问题 如何在邮递员响应中解决您没有访问端点所需的范围 - How to solve in postman response you don't have a required scope to access the endpoint 安装WAMP服务器后,Visual Studio的Localhost没有显示。 您如何使其工作? - The Localhost of visual studio doesn't show up when I have WAMP server installed. How do you make it work? 如何使wordpress中的PHP if语句没有换行符? - How do I make it so that the PHP if statements in wordpress don't have line breaks? 您如何在PHP中处理多个AJAX请求? - How do you handle multiple AJAX requests in PHP? 如何重新爱您没有权限访问此服务器上的/ &lt;...错误 - how to reslove You don't have permission to access /< on this server…error 禁止您无权访问此资源。 .htaccess 未重定向请求 - Forbidden You don't have permission to access this resource. .htaccess is not redirecting requests 您无权浏览服务器? - You don't have permission to browse the server? Apache-禁止您没有权限 - Apache - Forbidden You don't have permission jQuery-合并ajax请求,使它们拆分并替换数据 - jQuery - merging ajax requests as have them split up and replacing data 如果您知道自己有2个数据库项,每页5个。 如果用户请求页面,您如何知道该页面是否存在? - if you know you have 2 database items and 5 per page. If the user requests a page, how do you know if that page exisits or not?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM