简体   繁体   English

如何解决asp.net页面中的ajax发布jquery?

[英]how to resolve ajax post jquery in asp.net page?

when i add alert(file) line after ajax block code work but when i remove alert(file) code not work . 当我在ajax阻止代码工作后添加alert(file)行但当我删除alert(file)代码不起作用时。

this is work fine : 这工作正常:

 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);

    }

if i remove alert(file) line code web method not work. 如果我删除Alert(file)行代码Web方法不起作用。

this my c# asp.net code web method : 这是我的C#asp.net代码网络方法:

[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";
}

I see three (3) errors in your code: 我在您的代码中看到三(3)个错误:

        data: '{file: "' + file + '" }',
        datatype: "jsondata",
        async: "true",
  1. You are not sending a js object to the server. 您没有将js对象发送到服务器。
  2. jsondata is not a valid datatype. jsondata不是有效的数据类型。
  3. async is a boolean and you are instead assigning a string value. async是一个布尔值,而您要分配一个字符串值。

So the solution from here is: 因此,这里的解决方案是:

        data: {file: file }, // send a object
        datatype: "text",    // change to text because you are returning "ok" string
        async: true,         // remove the quotes 

Another suggestion is try with default headers: 另一个建议是尝试使用默认标题:

contentType: "application/json; charset=utf-8",

Try removing this line and see. 尝试删除此行并查看。

You're sending the request asynchronously,try to make false asynchronous. 您正在异步发送请求,请尝试使false异步。 for more information take a look at this 有关更多信息,请查看

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

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