JQuery AJAX syntax
Solution 1
How about this:
var id = empid;
$.ajax({
type: "POST",
url: "../Webservices/EmployeeService.asmx/GetEmployeeOrders",
data: "{empid: " + empid + "}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(result){
alert(result.d);
console.log(result);
}
});
Solution 2
Complete ajax syntax
var data="abc";
$.ajax({
type: "GET",
url: "XYZ",
data: {
"data":data,
},
dataType: "json",
//if received a response from the server
success: function( datas, textStatus, jqXHR) {
},
//If there was no resonse from the server
error: function(jqXHR, textStatus, errorThrown){
},
//capture the request before it was sent to server
beforeSend: function(jqXHR, settings){
},
//this is called after the response or error functions are finished
//so that we can take some action
complete: function(jqXHR, textStatus){
}
});
Solution 3
data
can either be a URL encoded string or an object:
data: {empid: empid},
OR
data: "empid=" + empid,
The docs say:
Data to be sent to the server. It is converted to a query string, if not already a string. It's appended to the url for GET-requests. See processData option to prevent this automatic processing. Object must be Key/Value pairs. If value is an Array, jQuery serializes multiple values with same key i.e. {foo:["bar1", "bar2"]} becomes '&foo=bar1&foo=bar2'.
Solution 4
This should work for you.
$.ajax({
type: "POST",
url: "../Webservices/EmployeeService.asmx/GetEmployeeOrders",
data: {empid: empid},
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(result) {
alert(result.d);
}
Solution 5
It's not. You're passing a string, you should be passing an object literal,e.g.
data: {"empid" : empid}
See the difference? Assuming empid is a variable with some sort of value, that should work fine. Alternatively you can do this
data: "empid="+empid
Nick
Software developer in the Boise, Idaho area. Interests: C#, C\C++, Objective-C Client-side scripting Design architecture, Patterns
Updated on July 09, 2022Comments
-
Nick almost 2 years
I am trying to find the correct syntax to pass a varible to my JQuery Post.
var id = empid; $.ajax({ type: "POST", url: "../Webservices/EmployeeService.asmx/GetEmployeeOrders", data: "{empid: empid}", contentType: "application/json; charset=utf-8", dataType: "json", success: function(result) { alert(result.d); }
I don't think the data: value is quite right. Someone set me straight?
Thanks!
-
nr5 over 4 yearsI am trying to hit an ajax request via postman. The ajax request is sending dataType:` json` and data: {loginId: "[email protected]", client: "698983"}. While going into the postman, I am trying to send body parameters as JSON with all the above info and Content-Type: application/json in headers but it fails with 500. Any help ?
-
Abhilash Singh Chauhan over 2 yearsWorked Well. Thanks