Javascript 从ajax错误函数内部访问post数据

Javascript 从ajax错误函数内部访问post数据,javascript,jquery,ajax,post,Javascript,Jquery,Ajax,Post,我有下面的javascript函数,它使用Ajax获取POST数据并向服务器发送POST请求 因此,我的实际观点是,由于某种原因,服务器正在发送一个500响应,因此执行点出现了错误:函数jqXHR、textStatus、errorshown、data,这里我想访问post_数据以向用户显示一些内容。。。。那么,我们可以在上面的ajax error函数中访问post_数据吗?如果有人寻找一种通用的方法来实现这一点,我是这样做的:如果您的处理程序函数的作用域不允许您访问某些变量,您可以在发送之前将它

我有下面的javascript函数,它使用Ajax获取POST数据并向服务器发送POST请求


因此,我的实际观点是,由于某种原因,服务器正在发送一个500响应,因此执行点出现了错误:函数jqXHR、textStatus、errorshown、data,这里我想访问post_数据以向用户显示一些内容。。。。那么,我们可以在上面的ajax error函数中访问post_数据吗?

如果有人寻找一种通用的方法来实现这一点,我是这样做的:如果您的处理程序函数的作用域不允许您访问某些变量,您可以在发送之前将它们添加到函数中的ajax对象本身。然后,您可以使用以下命令在ajax对象中检索它

function postData(post_data) {
    console.log(post_data, "----------->"); 
    // var data = post_data; // why ?
    var url = "/super/man/"
    $.ajax(
      {
        type: "POST",
        url: url,
        data: post_data,
        dataTpe: "json",
        success: function (response) { // pay attention to args and vars naming as it makes the code easier to read
          // use response
        },
        error: function (jqXHR, textStatus, errorThrown, data) {
          // handle error
          console.log(post_data); // and the magic happens
        },
      }
    );

  };
$.ajax{ url:“/dummyUrl”, beforeSend:functionjqXHR,plainObject{ plainObject.originalUrl='myValue'; }, 成功:功能反应{ $'output'.htmlresponse.responseText; }, 错误:函数{ $'output'.html'Bummer:发生错误!'; $'myValue'.htmlthis.originalUrl; }, }; 等待结果
如果有人在寻找一种通用的方法来实现这一点,我就是这样做的:如果您的处理函数的作用域不允许您访问某些变量,那么您可以在发送之前将它们添加到函数中的ajax对象本身。然后,您可以使用以下命令在ajax对象中检索它

$.ajax{ url:“/dummyUrl”, beforeSend:functionjqXHR,plainObject{ plainObject.originalUrl='myValue'; }, 成功:功能反应{ $'output'.htmlresponse.responseText; }, 错误:函数{ $'output'.html'Bummer:发生错误!'; $'myValue'.htmlthis.originalUrl; }, }; 等待结果
在这个问题上面,您的密钥数据类型错误,我已经修改了它。其次,post_数据在您的范围内,您可以访问它,而不会出现任何问题

    function postData(post_data) {
    console.log(post_data, "----------->"); 
    // var data = post_data; // why ?
    var url = "/super/man/"
    $.ajax(
      {
        type: "POST",
        url: url,
        data: post_data,
        dataType: "json",
        success: function (response) { // pay attention to args and vars naming as it makes the code easier to read
          // use response
        },
        error: function ( jqXHR jqXHR, textStatus, errorThrown) {
          // post data is in your scope you can easily access it
          console.log(post_data); // and the magic happens
        },
      }
    );

  };

在这个问题上面,您的密钥数据类型错误,我已经修改了它。其次,post_数据在您的范围内,您可以访问它,而不会出现任何问题

    function postData(post_data) {
    console.log(post_data, "----------->"); 
    // var data = post_data; // why ?
    var url = "/super/man/"
    $.ajax(
      {
        type: "POST",
        url: url,
        data: post_data,
        dataType: "json",
        success: function (response) { // pay attention to args and vars naming as it makes the code easier to read
          // use response
        },
        error: function ( jqXHR jqXHR, textStatus, errorThrown) {
          // post data is in your scope you can easily access it
          console.log(post_data); // and the magic happens
        },
      }
    );

  };

为什么不直接使用post_数据变量呢?它已经为您定义好了。你可以自由使用它。使用你的post_数据变量。你为什么不试试看呢?只需在错误回调函数中使用datavar。重命名回调参数或变量数据,因为回调参数在其自己的作用域中重写了它我已经检查过了。。。它表示未捕获引用错误:数据未定义,不使用变量数据。使用post_数据。为什么不直接使用post_数据变量呢?它已经为您定义好了。你可以自由使用它。使用你的post_数据变量。你为什么不试试看呢?只需在错误回调函数中使用datavar。重命名回调参数或变量数据,因为回调参数在其自己的作用域中重写了它我已经检查过了。。。它表示未捕获引用错误:数据未定义,不使用变量数据。使用post_数据。相同错误未捕获引用错误:未定义post_数据可以发布完整的控制台日志吗?VM1984:1未捕获引用错误:在eval eval at error happy_rules时未定义post_数据。js:78,:1:1 at Object.error happy_rules.js:78 at c jquery.min.js:3 at Object.fireWith[as rejectWith]jquery.min.js:3在k jquery.min.js:5在XMLHttpRequest.r jquery.min.js:5和第一个console.logpost_数据,------->?它输出什么?只是一个我需要的输出{id:-1,名称:1,描述:,…}相同错误未捕获引用错误:post_数据未定义您可以发布完整的控制台日志吗?VM1984:1未捕获引用错误:post_数据未在eval eval at error happy_规则中定义。js:78,:1:1 at Object.error happy_rules.js:78 at c jquery.min.js:3 at Object.fireWith[as rejectWith]jquery.min.js:3 at k jquery.min.js:5 at XMLHttpRequest.r jquery.min.js:5和第一个控制台.logpost_数据,------->?它输出什么?只是一个我需要的输出{id:-1,name:1,description:,…}