jquery post – unexpexted XHR in promise

huangapple go评论59阅读模式
英文:

jquery post - unexpexted XHR in promise

问题

I experience that when calling the same url xhr in the promise returns different objects

在使用 promise 调用相同的 URL 时,xhr 返回不同的对象

jquery post – unexpexted XHR in promise

jquery post – unexpexted XHR in promise

In the print screen of the console the first two lines are the first request (which returns HTTP 422). The XHR is the xhr object as expected.

在控制台的打印屏幕上,前两行是第一个请求(返回 HTTP 422)。XHR 是预期的 xhr 对象。

The last two lines are the next request (which returns HTTP 200) but now the XHR object is the actual response? Why is that?

而最后两行是下一个请求(返回 HTTP 200),但现在 XHR 对象是实际的响应?为什么会这样?

I have been pulling my hair out and can't really understand why that is happening? I'm just calling a jquery post. What can inflict that? I'm not doing anything fancy on the frontend with jquery. Just using the native $.post without extending anything.

我已经快被搞疯了,真的不明白为什么会发生这种情况?我只是调用了一个 jQuery 的 POST 请求。是什么导致了这种情况?我在前端并没有使用任何复杂的 jQuery 操作,只是使用原生的 $.post,没有进行任何扩展。

I can't really see what I can do on the backend to change the XHR object to the response in jquery?

我真的不知道在后端上可以做什么来将 XHR 对象更改为 jQuery 中的响应对象?

英文:

I experience that when calling the same url xhr in the promise returns different objects

jquery post – unexpexted XHR in promise

In the print screen of the console the first two lines are the first request (which returns HTTP 422). The XHR is the xhr object as expected

The last two lines are the next request (which returns HTTP 200) but now the XHR object is the actual response? Why is that?

I have been pulling my hair out and can't really understand why that is happening? I'm just calling a jquery post. What can inflict that? I'm not doing anything fancy on the frontend with jquery. Just using the native $.post without extending anything

I can't really see what I can do on the backend to change the XHR object to the response in jquery?

$.post(url, post).always(xhr=>{
	const res = xhr.responseText ? JSON.parse(xhr.responseText) : [];
	console.log('xhr', xhr)
	console.log('res',res)
});

答案1

得分: 1

请阅读jQuery文档:

> 注意:deferred.always()方法接收用于.resolve()或.reject() Deferred对象的参数,这些参数通常是非常不同的。因此,最好仅在不需要检查参数的操作中使用它。在所有其他情况下,请使用明确的.done()或.fail()处理程序,因为参数将具有知名的顺序。

还要了解有关AJAX快捷方式的信息,您将在这里找到适当的代码示例:
https://api.jquery.com/category/ajax/shorthand-methods/

var jqxhr = $.get( "example.php", function() {
  alert( "success" );
})
  .done(function() {
    alert( "second success" );
  })
  .fail(function() {
    alert( "error" );
  })
  .always(function() {
    alert( "finished" );
  });

不要将 always() 用于UI/业务逻辑,只用于UI清理,比如移除加载器。在您的POST调用成功后,如果要执行一些操作,请使用片段的第一个或第二个回调。

英文:

Read jQuery documentation please:

> Note: The deferred.always() method receives the arguments that were used to .resolve() or .reject() the Deferred object, which are often very different. For this reason, it's best to use it only for actions that do not require inspecting the arguments. In all other cases, use explicit .done() or .fail() handlers since the arguments will have well-known orders.

Read also about AJAX shortcuts, here you'll find a proper code example:
https://api.jquery.com/category/ajax/shorthand-methods/

var jqxhr = $.get( "example.php", function() {
  alert( "success" );
})
  .done(function() {
    alert( "second success" );
  })
  .fail(function() {
    alert( "error" );
  })
  .always(function() {
    alert( "finished" );
  });

You shouldn't use always() for UI/business logic, only for the UI cleaning like removing your loaders.
Use the first or seconds callbacks from the snippet when you want to do some things after your POST call is successful.

huangapple
  • 本文由 发表于 2023年5月17日 17:13:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76270422.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定