0

I don't know what's wrong with this code. I am not able to implement it properly. Thanks in advance for help. This is what I've tried so far and stuck

<script type="text/javascript">
            $(document).ready(function () {
                for (var key in localStorage) {
                    GetQuickVL(key);
                }
            });
            function GetQuickVL(key) {
                if (key.substring(0, 4) == "vhs-") {
                    $.ajax({
                        type: "POST",
                        url: "/QuickViewList.aspx/GetQuickVD",
                        data: '{key: ' +'1' + '}',
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: OnSuccess,
                        failure: function (response) {
                            alert(response.response);
                        },
                        error: function (response) {
                            alert(response.error);
                        }
                    });
                }
            }
            function OnSuccess(response) {
                alert('df');
            }
</script>

and the C-Sharp code is

[WebMethod]
public int GetQuickVD(int key)
{
    return key;
}
4
  • Is there any error in your console? Commented Mar 28, 2014 at 11:22
  • i'm getting a weird error but i can't post the image because I don't have 10 reputation points Commented Mar 28, 2014 at 11:23
  • 1
    @user3472352, no need to post the image - text of the error will suffice Commented Mar 28, 2014 at 11:26
  • i'm getting something like function () { if (u) { var t = u.length; (function i(t) { b.each(t, function (t, n) { var r = b.type(n); "function" === r ? e.unique && p.has(n) || u.push(n) : n && n.length && "string" !== r && i(n) }) })(arguments), n ? o = u.length : r && (s = t, c(r)) } return this } Commented Mar 28, 2014 at 11:28

2 Answers 2

1

key is an int. You've passed it as a string:

'{key: ' +'1' + '}',

Either do:

{key: 1 },

Or make your web method take an object or string as its parameter:

[WebMethod]
public int GetQuickVD(object key)
{
    return Convert.ToInt32(key);
}

Here is a complete working sample (I just tested this). Adapt to suit your needs:

WebService:

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)] 
[System.Web.Script.Services.ScriptService] // <- ** MAKE SURE THIS IS UNCOMMENTED!
public class WebService1 : System.Web.Services.WebService
{
    [WebMethod]
    public int GetQuickVD(int key)
    {
        return key;
    }
}

Aspx page:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $('.foo').click(function () {
            $.ajax({
                type: "POST",
                url: "WebService1.asmx/GetQuickVD",
                data: '{key: ' + '1' + '}',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function () {
                    alert("success");
                }
            });
        })
    });
</script>
<a href="#" class="foo">click me</a>
Sign up to request clarification or add additional context in comments.

2 Comments

I've edited the answer with a fully working sample. It should be easy to plug the rest of your code in. Do you have this line: [System.Web.Script.Services.ScriptService] uncommented or added to your web service? The url property in your ajax call looks a bit weird too. Can you post the code behind to that file? You should move your web method into its own webservice really.
The code of DGibbs looks good. Should work. Here's another example I've written about 2 years ago: tomot.de/en-us/article/8/asp.net/… I can't find any big difference. Maybe you have more luck or the problem is elsewhere
0

if the method is in your code behind (.aspx.cs file) try declaring the GetQuickVD method as static. so it's public static int GetQuickVD(int key).

1 Comment

i wonder if anyone can tell why I'm getting error on doing data:'{key:'+'1,1,1,1,'+'}';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.