JQuery AJAX 语法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/780038/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JQuery AJAX syntax
提问by Nick
I am trying to find the correct syntax to pass a varible to my JQuery Post.
我试图找到正确的语法来将变量传递给我的 JQuery 帖子。
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?
我不认为 data: value 是正确的。有人让我直截了当?
Thanks!
谢谢!
回答by Jose Basilio
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);
}
});
回答by Paolo Bergantino
data
can either be a URL encoded string or an object:
data
可以是 URL 编码字符串或对象:
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'.
要发送到服务器的数据。如果不是字符串,则将其转换为查询字符串。它附加到 GET 请求的 url。请参阅 processData 选项以防止此自动处理。对象必须是键/值对。如果 value 是一个数组,jQuery 将序列化多个具有相同键的值,即 {foo:["bar1", "bar2"]} 变为 '&foo=bar1&foo=bar2'。
回答by Mangy 007
Complete ajax syntax
完整的ajax语法
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){
}
});
回答by ólafur Waage
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);
}
回答by bdl
It's not. You're passing a string, you should be passing an object literal,e.g.
它不是。您正在传递一个字符串,您应该传递一个对象文字,例如
data: {"empid" : empid}
See the difference? Assuming empidis a variable with some sort of value, that should work fine. Alternatively you can do this
看到不同?假设empid是一个具有某种值的变量,它应该可以正常工作。或者你可以这样做
data: "empid="+empid
回答by LCJ
Though not a direct answer to your question, following is the common function approach used in one of our projects for jquery calls
虽然不是您问题的直接答案,但以下是我们的一个项目中用于 jquery 调用的常用函数方法
The Proxy method takes an existing function and returns a new one with a particular context.
Proxy 方法采用现有函数并返回具有特定上下文的新函数。
Syntaxes
语法
$(selector).proxy(function,context)
$(selector).proxy(context,name)
CODE
代码
dpInvokeAsync: function (serviceRequest, input, requestType, successCallBack) {
var url = BASE_URL + serviceRequest;
$.ajax({
type: requestType,
url: url,
async: true,
data: input,
dataType: 'json',
success: $.proxy(successCallBack, this),
error: $.proxy(this.handleFailure, this)
});
}
this.dpInvokeAsync('App/ShowParts', searchCriteria, 'Post',
function (result) { alert(result);}
);
REFERENCES
参考
回答by Chad Grant
if you want to send a JSON string to the server
如果你想向服务器发送一个 JSON 字符串
data: "{empid: " + empid + "}"
if you want to send querystring params (?empid=123)
如果你想发送查询字符串参数 (?empid=123)
data: {empid : empid}
回答by khushwant
you can use the following.
您可以使用以下内容。
var id = empid;
$.ajax({
type: "POST",
url: "../Webservices/EmployeeService.asmx/GetEmployeeOrders",
data: "var1=val1&var2=val2&var3=val3&var4=val4&var5=val5",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
alert(result.d);
}
回答by Srikar Doddi
$(document).ready(function() {
$.ajax({
type: "POST",
url: "Webservices/EmployeeService.asmx/GetEmployeeOrders",
data: "{'EmployeeId':'empid'}", **<-- see the single quotes**
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
alert(msg);
}
});
});