ajax库 vuejs,VueJs Ajax Data Mapping

In a Vue page, I have a call to get data using Ajax when the mounted() event is fired. The code recreates the existing Pager using a new Pager object where it has to pass in all the parameters in the constructor to reconstruct it.

If I don't do this, vm.Pager is just an Object and does not have some needed methods, and fails the prop type check that it gets passed to.

axios.post("/Home/GetList", vm.Pager)

.then(function (result)

{

var p = result.data.Pager;

vm.Pager = new Pager(p.PageSize, p.CurrentRecord, p.TotalCount);

// etc. (Pager has additional fields...)

vm.ItemList = result.data.ListItems;

})

.catch(function (error)

{

alert(error);

});

In the knockoutjs, there was a mapping function and you could what tell it what types to map without having to recreate the object. This was convenient, particularly for more complicated or nested Ajax data.

Is there a better way to do this in Vue (or javascript) where it maps the type from the Ajax without having to recreate it?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的Java代码示例,用于处理Ajax请求并返回数据: ```java import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class AjaxServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 处理GET请求 String data = "这是从服务器返回的数据"; response.getWriter().write(data); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 处理POST请求 String data = "这是从服务器返回的数据"; response.getWriter().write(data); } } ``` 在这个示例中,我们创建了一个名为AjaxServlet的Java Servlet,它有两个方法:doGet和doPost,用于处理GET和POST请求。 在这两个方法中,我们都返回了一个简单的字符串,表示从服务器返回的数据。在实际应用中,您可能需要从数据或其他数据源中检索数据,并将其作为响应发送回客户端。 要将此Servlet映射到特定的URL,请在您的web.xml文件中添加以下代码: ```xml <servlet> <servlet-name>AjaxServlet</servlet-name> <servlet-class>your.package.name.AjaxServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>AjaxServlet</servlet-name> <url-pattern>/ajax</url-pattern> </servlet-mapping> ``` 这将使AjaxServlet类可用于处理来自客户端的/ajax请求。您可以使用jQuery等JavaScript来发出此请求。例如: ```javascript $.ajax({ url: "/ajax", success: function(data) { console.log(data); // 输出从服务器返回的数据 } }); ``` 当客户端发出此请求时,AjaxServlet将处理请求并返回响应数据。客户端可以使用回调函数来处理响应数据。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值