1

I have following code snippet in which I want to return value from ajax.but I am getting following exception

Request format is invalid

[WebMethod]
[ScriptMethod(ResponseFormat=ResponseFormat.Json)]
public string HelloWorld(string name) {
    return "Hello World"+name;
}


 $(document).ready(function () {
            function checkUser2(user) {
                var result;
                $.ajax({

                    type: "POST",
                    async: false,
                    url: "WebService.asmx/HelloWorld",
                    dataType: "json",
                    contentType:"application/json",
                    data: { name: user},
                    success: function (data) {
                        result = data;
                    }
                });
                return result;
            }
            $("#check").click(function () {
                alert(checkUser2("test"));
            });
        });

EDIT

If you have other way for this please share some link or code

3 Answers 3

2

try this

   $.ajax({

               ...
                data: "{ 'name': 'user'}",
                ...
            });
Sign up to request clarification or add additional context in comments.

2 Comments

I am getting this Invalid JSON primitive: name
Just return a straight string - the decorators will return it as json for you. The answer by @cranberies is leading you the wrong direction.
1

try this :

    [WebMethod]
    [ScriptMethod(ResponseFormat=ResponseFormat.Json)]
    public string HelloWorld(string name) {
        return "{'message':'Hello World'}";
    }


    -----
    <script type="text/javascript">
$(document).ready(function () {
            function checkUser2(user) {
                var result;
                $.ajax({
                    type: "POST",
                    async: false,
                    url: "WebService.asmx/HelloWorld",
                    dataType: "json",
                    data:{name:user},
                    success: function (data) {
                        result = data.message;
                    }
                });
                return result;
            }
            $("#check").click(function () {
                alert(checkUser2("test"));
            });
        });
</script>

json for ASP : http://code.google.com/p/aspjson/

3 Comments

Now,I am getting this exception Invalid JSON primitive: name
test with : data:'name='+user,
remove contentType:"application/json", it work for me , im using php i think it will work for you see update in my response
1

I figured out the issue.Below is the complete source code(no changes in webservice)

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
    <title></title>
    <script type="text/javascript">
        $(document).ready(function () {
            function checkUser2(name, callback) {
                $.ajax({

                    type: "POST",
                    async: tr,
                    // url: "Handler.ashx",
                    url: "WebService.asmx/HelloWorld",
                    data: "{name:'" + name + "'}",
                    dataType: "json",
                    contentType: "application/json",
                    success: function (data) {
                        callback(data.d);
                    }
                });
            }
            $("#check").click(function () {
                checkUser2("test", function (d) {
                    var a = d;
                    alert(a);
                });
            });
        });


    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <input type="button" name="check" value="check " id="check" />
    </div>
    </form>
</body>
</html>

Comments

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.