简体   繁体   English

如何在不使用Ajax的重定向的情况下从不同的php页面请求数据?

[英]How to request data from different php page without a redirect that works with Ajax?

So I have this one CakePHP web application that I'm trying to refactor. 因此,我有一个要重构的CakePHP Web应用程序。
There is a large portion of code that usually works like this: 通常有很大一部分代码是这样的:
Eg "Flag": 例如“标志”:
User clicks flag button 用户单击标志按钮
Redirect the user to the flag action 将用户重定向到标志动作
Flag action flags post 标记动作标志发布
Redirect user to post that was just flagged. 重定向用户以发布刚刚标记的帖子。

I am also trying to enhance it with JavaScript like so: 我还试图像这样用JavaScript增强它:
User clicks flag button 用户单击标志按钮
Javascript makes AJAX request to that action JavaScript对该动作发出AJAX请求
Call returns with a status that JS inteprets 呼叫返回状态为JS解释

Is there some way to streamline this sort of flow so that it is easily testable and can be covered with a javascript that doesn't need redirection? 是否有某种方法可以简化这种流程,使其易于测试,并且可以用不需要重定向的JavaScript覆盖?

Lookup jQuery's .post() function. 查找jQuery的.post()函数。 That lets you post to a URL and handle (in a callback function) whatever results the server returns. 这样,您就可以发布到URL并处理(在回调函数中)服务器返回的任何结果。 The server can return some simple data to indicate success or failure, or it can return an HTML fragment to be placed somewhere on the page (under control of your Javascript code), or really anything else you want. 服务器可以返回一些简单的数据来指示成功或失败,也可以返回HTML片段以放置在页面的某个位置(在Javascript代码的控制下),或者实际上是您想要的任何其他内容。

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

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