将按钮的id传递给jquery post方法

时间:2017-08-28 10:23:26

标签: jquery vb.net

我正在尝试传递我点击的ID按钮的asp.net我正在尝试以下内容但编译器似乎不喜欢它抱歉我的jquery有点生疏,这是我的代码:

<script type = "text/javascript">
    function AddToFavourites() {
        var element = $(this);

        $.ajax({
            type: "POST",
            url: "/dynamic/Favourite.aspx/AddToFavourites",
            data: '{productId:'+ element.attr('id') + "}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: OnSuccess,
            failure: function(response) {
                alert(response.d);
            }
        });
    }
    function OnSuccess(response) {
        alert(response.d);
    }
 </script>

这是这一行,它失败了:

  

数据:'{productId:'+ element.attr('id')+“}”,

也是他们在这次调用之后的一种方式返回结果,一旦我检查了该人喜欢该产品的数据库,就将fa-heart图标更改为红色。

编辑3

显示控制台.log的输出

VM41:1 POST http://localhost:52448/dynamic/Favourite.aspx/AddToFavourites 500 (Internal Server Error)
(anonymous) @ VM41:1
send @ jquery.js:9664
ajax @ jquery.js:9215
AddToFavourites @ Aspen:24
onclick @ Aspen:919

非常奇怪,因为我在我的代码后面有该函数的文件

<WebMethod()> _
<ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
Public Shared Function AddToFavourites(productId As String) As String
Dim constr As String = ConfigurationManager.ConnectionStrings("ConnStr").ConnectionString
     Using con As New SqlConnection(constr)
        Dim strQuery As String
        'set up the sql command
        Dim oSqlCommand As New SqlClient.SqlCommand
        '   oSqlCommand.Connection = oSqlConn

        Dim oDataAdapter As New SqlClient.SqlDataAdapter
        Dim oLinesTable As New DataTable

        Dim strProductID As String
        strProductID = productId.Replace("product_", "")

     End Using

    Return ""

End Function

1 个答案:

答案 0 :(得分:0)

更改您的专线data: '{productId:'+ element.attr('id') + "}",

使用:

data: {'productId': element.attr('id') },