How to send data in request body with a GET when using jQuery $.ajax()
In general, that's not how systems use GET requests. So, it will be hard to get your libraries to play along. In fact, the spec says that "If the request method is a case-sensitive match for GET or HEAD act as if data is null." So, I think you are out of luck unless the browser you are using doesn't respect that part of the spec.
You can probably setup an endpoint on your own server for a POST ajax request, then redirect that in your server code to a GET request with a body.
If you aren't absolutely tied to GET requests with the body being the data, you have two options.
POST with data: This is probably what you want. If you are passing data along, that probably means you are modifying some model or performing some action on the server. These types of actions are typically done with POST requests.
GET with query string data: You can convert your data to query string parameters and pass them along to the server that way.
url: 'somesite.com/models/thing?ids=1,2,3'
jQuery posting valid json in request body
An actual JSON request would look like this:
data: '{"command":"on"}',
Where you're sending an actual JSON string. For a more general solution, use JSON.stringify()
to serialize an object to JSON, like this:data: JSON.stringify({ "command": "on" }),
To support older browsers that don't have the JSON
object, use json2.js which will add it in.What's currently happening is since you have
processData: false
, it's basically sending this: ({"command":"on"}).toString()
which is [object Object]
...what you see in your request. Send a GET request with a body in JavaScript (XMLHttpRequest)
No, it is not possible to send a GET request with a body in JavaScript.
That is correct. This is defined in the specification:it looks like the payload is simply not sent
Also a request via the Fetch API does not allow a body. From the specification:The
send(body)
method must run these steps:...
- If the request method is
GET
orHEAD
, set body to null.
The best would be if the API could be fixed.
- If either init["body"] exists and is non-null or inputBody is non-null, and request’s method is
GET
orHEAD
, then throw a TypeError.
If that is not possible, you could add a server-side script that you can use as a proxy that passes the requests to the API. You can than call this script with a GET request with the data in the URL instead of the body or using a POST request with a body. This script then can make the GET request with a body (as long as the chosen language supports it).
jQuery get request with json body to a REST api
You should use the $.param
function when you build a GET request (and not the JSON.Stringify
function).
data: $.param({'last_id' : 1650 }),
$.ajax({ type: 'GET', url: '/', dataType: 'json', processData: false, data: $.param({'last_id' : 1650 }), success: function(resp){ console.log(resp); }});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
How to submit JSON Data by Request Body in jQuery?
Here is the right code for your desired out put.
$.ajax({
url : "/",
type: "POST",
data: JSON.stringify([
{id: 1, name: "Shahed"},
{id: 2, name: "Hossain"}
]),
contentType: "application/json; charset=utf-8",
dataType : "json",
success : function(){
console.log("Pure jQuery Pure JS object");
}
});
Your must convert JS Object to String and JSON.stringify(JSObject)
is the method responsible for that. How to Pass Data in ajax call using GET Method in javascript
try this
$.ajax({
url: Globals.baseURL + "rest/grid/"+cuboid_id,
type: "GET",
dataType: "application/json",
data: {some_query_var : JSON.stringify(data)},
contentType: "application/json",
success: function(result){
console.log("***********************++++++++++++++*************************");
console.log(JSON.stringify(result));
//assert.equal(result !=null,true,"Response should not be null");
//assert.equal(result[0].error,"Whitebaord ID NOT FOUND","InValid Whiteboard ID");
assert.equal(1,1);
done();
}
});
"dataType json" in ajax jquery doesn't mean for formating JSON string in "data" attribute .. you still need the query variable passing in the "data" attribute. wich is in this is case i use some_query_var.
Related Topics
Jquery Returning "Parsererror" for Ajax Request
Why Let and Var Bindings Behave Differently Using Settimeout Function
Window.Location.Reload with Clear Cache
React Doesn't Reload Component Data on Route Param Change or Query Change
When Do I Need to Specify the JavaScript Protocol
D3.JSON Method Doesn't Return My Data Array
Cannot Set Property 'Innerhtml' of Null
How to Show a Spinner While Loading an Image via JavaScript
Angular 2 Router Event Listener
How to Export Socket.Io into Other Modules in Nodejs
Why How to Not Throw Inside a Promise.Catch Handler
JavaScript Object Literals Syntax Error
Why Do Empty JavaScript Arrays Evaluate to True in Conditional Structures