简体   繁体   English

jQuery ajax发送数据

[英]jquery ajax sending data

contentType: "text/html; charset=utf-8",
url:"secret.aspx?plu="+$("#Text1").val()+"&gh="+$("#TextBox1").val()+"&sid="+$("#TextBox2").val(),
processData: false,
dataType: "html",
 success: function(data)

is it the above syntax correct to send the data recieved by the code below 上面的语法是否正确,以发送以下代码接收的数据?

string sid=Request.QueryString["sid"];
string id = Request.QueryString["plu"];
int ide =Convert.ToInt32(Request.QueryString["gh"]);
Response.write(sid);
Response.end();

or is there any other way to achieve the same 还是有其他方法可以达到相同目的

The only problem with that request is that it will break if you have any special characters in your input values. 该请求的唯一问题是,如果您在输入值中包含任何特殊字符,它将中断。

A solution to that would be to pass a data object: 一种解决方案是传递数据对象:

type:"GET",
url:"secret.aspx",
data: {
    plu : $("#Text1").val(),
    gh : $("#TextBox1").val(),
    sid : $("#TextBox2").val()
},
dataType: "html",

This encodes special characters to avoid breaking the key/value format. 这将对特殊字符进行编码,以避免破坏键/值格式。 Alternatively you could keep them in the url but wrap each in encodeURIComponent() , which would have the same effect. 或者,您可以将它们保留在url但将它们包装在encodeURIComponent() ,这将具有相同的效果。

You need to serialize your form data into the 'data' option of the ajax method. 您需要将表单数据序列化为ajax方法的'data'选项。 Also, specify the type of request as GET if you want to use the query string. 另外,如果要使用查询字符串,请将请求的类型指定为GET。

type: 'GET'
contentType: "text/html; charset=utf-8",
url:'secret.aspx',
processData: false,
dataType: "html",
data: $('#myForm').serialize(),

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

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