简体   繁体   中英

jquery ajax : what is the actual request sent?

What is the actual url sent to the server when I use the jquery ajax? 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".

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.

You're sending the data in the request body of an HTTP post. The data is the data that you assigned to the data parameter. You don't need to round-trip it through jQuery's ajax methods. You've got it already. It's:

{name:'Smith',age:10}

does jQuery's interpretation of your data really matter?

The settings object is fully populated when beforeSend is called:

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

$.ajax({ type: "POST", ... }) will log

response.php
name=Smith&age=10

and type: "GET"

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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