我遵循了this链接中所述的教程。在下面的代码中,由于某种原因,数据没有作为参数附加到url中,但是如果我使用/?field1="hello"
将它们直接设置到url中,它就可以工作。
$.ajax({
url: 'superman',
type: 'POST',
data: { field1: "hello", field2 : "hello2"} ,
contentType: 'application/json; charset=utf-8',
success: function (response) {
alert(response.status);
},
error: function () {
alert("error");
}
});
发布于 2013-09-09 19:23:50
我建议您在简单的情况下使用jQuery的$.post
或$.get
语法:
$.post('superman', { field1: "hello", field2 : "hello2"},
function(returnedData){
console.log(returnedData);
});
如果您需要捕获失败案例,只需执行以下操作:
$.post('superman', { field1: "hello", field2 : "hello2"},
function(returnedData){
console.log(returnedData);
}).fail(function(){
console.log("error");
});
此外,如果您总是发送JSON字符串,则可以在最后使用$.getJSON或$.post,并在最后再添加一个参数。
$.post('superman', { field1: "hello", field2 : "hello2"},
function(returnedData){
console.log(returnedData);
}, 'json');
发布于 2013-12-15 19:35:44
function FillData() {
var param = $("#<%= TextBox1.ClientID %>").val();
$("#tbDetails").append("<img src='Images/loading.gif'/>");
$.ajax({
type: "POST",/*method type*/
contentType: "application/json; charset=utf-8",
url: "Default.aspx/BindDatatable",/*Target function that will be return result*/
data: '{"data":"' + param + '"}',/*parameter pass data is parameter name param is value */
dataType: "json",
success: function(data) {
alert("Success");
}
},
error: function(result) {
alert("Error");
}
});
}
发布于 2013-09-09 19:18:50
在POST request中,参数在请求的主体中发送,这就是为什么您在URL中看不到它们的原因。
如果您想要查看它们,请更改
type: 'POST',
至
type: 'GET',
请注意,浏览器具有开发工具,可以让您查看代码发出的完整请求。在Chrome中,它在“网络”面板中。
https://stackoverflow.com/questions/18697034
复制相似问题