访问“数据"成功功能内的AJAX请求中的对象 [英] Access "data" object in AJAX request inside success function

查看:64
本文介绍了访问“数据"成功功能内的AJAX请求中的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何访问在 $.ajax()请求中使用的 data 对象?

How can I access the data object used in a $.ajax() request?

$.ajax({
    url: "post.php",
    type:'POST',
    data:{
        el1: $('#el1').val(),
        ...
        el72: $('#el72').val()
    },
    success: function(res,status){
        //
    }
});

推荐答案

您可以使用 this 从成功回调中访问传递给$ .ajax的已处理选项,只要您没有不要绑定功能,使用箭头功能或使用上下文选项.

You can access the processed options that you passed into $.ajax from within the success callback using this, as long as you didn't bind the function, use an arrow function, or use the context option.

console.log(this.data); // for POST
console.log(this.url); // for GET, but you'll have to parse out the url portion

您会注意到,虽然它现在位于参数化字符串中,而不是对象中,因为这是将其发送到服务器的方式.参见此处,了解将其转换回对象的方法:将URL参数转换为JavaScript对象

You'll notice though that it's in a parameterized string rather than an object now because that's how it is sent to the server. See here for ways to convert it back to an object: Convert URL parameters to a JavaScript object

我只会使用一个变量.

这篇关于访问“数据"成功功能内的AJAX请求中的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆