简体   繁体   English

jquery ajax:实际发送的请求是什么?

[英]jquery ajax : what is the actual request sent?

What is the actual url sent to the server when I use the jquery ajax? 当我使用jQuery Ajax时,发送到服务器的实际URL是什么? And how do I access that value? 我该如何获得该价值? For example, consider the following code: 例如,考虑以下代码:

<script>
$.ajax({
type: "POST",
dataType: "json",
url: "response.php",
data: {name:'Smith',age:10},
success: function(data) {
    ...
}
beforeSend: function(){
    console.log(url); 
    // what do I put here to see what is being sent
    // I am expecting to see "response.php?name=Smith&age=10"
}
...

So essentially what variable holds "response.php?name=Smith&age=10". 因此,基本上什么变量保存“ response.php?name = Smith&age = 10”。

Thanks. 谢谢。

No variable holds 没有变量保持

response.php?name=Smith&age=10

because you aren't sending the data as a query string. 因为您没有将数据作为查询字符串发送。 This would happen if you issued a GET request, but doesn't with a POST request. 如果您发出GET请求,但没有POST请求,则会发生这种情况。

You're sending the data in the request body of an HTTP post. 您正在HTTP帖子的请求正文中发送数据。 The data is the data that you assigned to the data parameter. 数据是您分配给data参数的data You don't need to round-trip it through jQuery's ajax methods. 您不需要通过jQuery的ajax方法来回传递它。 You've got it already. 你已经知道了 It's: 它的:

{name:'Smith',age:10}

does jQuery's interpretation of your data really matter? jQuery对数据的解释真的重要吗?

The settings object is fully populated when beforeSend is called: 调用beforeSend时,将完全填充settings对象:

beforeSend: function(jqXHR, settings) {
   console.log(settings.url);
   console.log(settings.data);  
}

$.ajax({ type: "POST", ... }) will log $ .ajax({type:“ POST”,...})将记录

response.php
name=Smith&age=10

and type: "GET" 并输入:“ GET”

response.php?name=Smith&age=10
undefined

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

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