C# 如何在asp.net页面中解析ajax post jquery?
当我在ajax块代码之后添加alertfile行时有效,但当我删除alertfile代码时无效 这是很好的工作:C# 如何在asp.net页面中解析ajax post jquery?,c#,jquery,asp.net,ajax,C#,Jquery,Asp.net,Ajax,当我在ajax块代码之后添加alertfile行时有效,但当我删除alertfile代码时无效 这是很好的工作: function Delete(file) { $.ajax({ type: "POST", url: "Image.aspx/Delete", data: '{file: "' + file + '" }', contentType: "application/jso
function Delete(file) {
$.ajax({
type: "POST",
url: "Image.aspx/Delete",
data: '{file: "' + file + '" }',
contentType: "application/json; charset=utf-8",
datatype: "jsondata",
async: "true",
success: function (response) {
$("#statusBox").text("ok"); //alert(response.d);
},
error: function (response) {
$("#statusBox").text("error"+response.text);
// alert(response.status + ' ' + response.statusText);
},
complete: function () {
// $("#statusBox").text("completed");
}
});
alert(file);
}
如果我删除alertfile行代码,web方法将不起作用
此my c asp.net代码web方法:
[WebMethod]
public static string Delete(string file)
{
try
{
// int lastSlash=file.ind
// Lesson learnt - always check for a valid URI
if (Uri.IsWellFormedUriString(file, UriKind.Absolute))
{
Uri uri = new Uri(file);
file = (uri.LocalPath);
}
//file= file.Remove(0)
//File.Delete(file);
File.Delete(HttpContext.Current.Server.MapPath(@"~\" + file.Replace("/", @"\")));
}
catch (Exception ex)
{
return ex.Message;
}
return "ok";
}
我在您的代码中看到三个错误:
data: '{file: "' + file + '" }',
datatype: "jsondata",
async: "true",
您没有向服务器发送js对象。
jsondata不是有效的数据类型。
async是一个布尔值,您可以指定一个字符串值。
因此,这里的解决方案是:
data: {file: file }, // send a object
datatype: "text", // change to text because you are returning "ok" string
async: true, // remove the quotes
另一个建议是尝试使用默认标题:
contentType: "application/json; charset=utf-8",
请尝试删除此行,然后查看。如果要异步发送请求,请尝试将false设为异步。
有关详细信息,请查看我看不出添加alertfile会如何影响它前面的代码,但请定义它不起作用,会发生什么情况?浏览器控制台中是否有任何错误?按F12并选择console选项卡。我有另一个页面login.aspx的代码,但工作正常。没有看到您的后端代码您返回的是字符串而不是json,因此您可以改为文本。我的问题是。为什么如果我删除了alertfile行,我的代码不起作用,但添加了这一行之后就可以了。当文件名有空间时,我有一个问题要删除。str=str.replace/\s/g,;使用str=str.replace/\s/g;?时,使用此选项可从文件名中删除空格?在我的web方法中,在生成ajax请求数据之前,请在javascript上使用它:“{file:'+file.replace/\s/g,+'}”,