jquery ajax 显示“未找到”错误

本文关键字:未找到 错误 ajax 显示 jquery | 更新日期: 2023-09-27 18:32:32

我正在尝试使用 jquery ajax 将数据从客户端传递到服务器 side.it 应该非常容易,它是.我在家里添加了一个静态方法.aspx并使用 ajax 将数据发布在那里.但我不知道为什么它总是说找不到错误。请有人可以帮助我吗? 这是我的 Ajax 代码。

<script type="text/javascript">
   $(document).ready(function () {
       $('#form1home a').click(function () {
           var position = $(this).parent().attr("id");
           $.ajax({
               type: 'POST',
               url: 'home.aspx/clickaction',
               contentType: 'application/json',
               dataType: 'json',
               data:  JSON.stringify({ "position":position }),
               success: function () {
               },
               error: function (request, status, error) {
                   alert(request+" "+error);
               }
           });
       });
   });

我还给出了我在 C# 中使用的静态方法。

[WebMethod]
public static void clickaction(string position)
{
    DataBaseClass db = new DataBaseClass();
    string qry = "select * from cms_tbl where position='" + position + "' and shopid='" + HttpContext.Current.Session["shopid"].ToString() + "'";
    DataTable dt = db.ConnectDataBaseReturnDT(qry);
    if (dt.Rows[0]["type"].ToString() == "product")
    {
        string path = "product.aspx?id=" + dt.Rows[0]["items"].ToString() + "&shop=" + HttpContext.Current.Session["shopid"].ToString();
        HttpContext.Current.Response.Redirect(path);
    }
    else if (dt.Rows[0]["type"].ToString() == "subcat")
    {
        string path = "productdisplay.aspx?id=" + dt.Rows[0]["items"].ToString() + "&sid=" + HttpContext.Current.Session["shopid"].ToString();
        HttpContext.Current.Response.Redirect(path);
    }
    else if (dt.Rows[0]["type"].ToString() == "brand")
    {
        string path = "productdisplay.aspx?bid=" + dt.Rows[0]["items"].ToString() + "&sid=" + HttpContext.Current.Session["shopid"].ToString();
        HttpContext.Current.Response.Redirect(path);
    }
}

jquery ajax 显示“未找到”错误

我唯一建议您检查的是从其父属性中获取的位置值

var position = $(this).parent().attr("id");

通过警报检查它。