Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hi, I need to Send and Receive JSON objects to Web Service Methods using jQuery AJAX in ASP.Net
But when ever I am executing the below code, getting [object Object] error. Please help.

Html code:-

XML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script language="javascript" type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>

   <script type="text/javascript" src="Scripts/json2.js"></script>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script type="text/javascript">
        function fnSentMail() {debugger;

            var userid = document.getElementById("txtuid").value;
            var txtpwd = document.getElementById("txtuid").value;


            $.ajax
    ({
        type: "POST",
        url: "Service/testservice.asmx/_testserv",
        data: "{'userid':'" + userid + "','password': '" + txtpwd  + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {debugger;
            alert(1);
        },
        error: function (ex) {
            alert(ex.toString());
        }
    });
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>

    <table>
    <tr>
    <td>User ID</td>
    <td>
        <asp:TextBox ID="txtuid" runat="server"></asp:TextBox></td>
    </tr>
    <tr>
    <td>Password</td>
    <td>
        <asp:TextBox ID="txtpwd" runat="server"></asp:TextBox></td>
    </tr>
    <tr>
    <td colspan="2">
        <asp:Button ID="btnLogin" runat="server" Text="Button" OnClientClick="return fnSentMail()"  />
    </td>
    </tr>
    </table>
    </div>
    </form>
</body>
</html>



testservice.asmx.cs:-
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Web.Script.Services;
using System.IO;
using System.Collections.Specialized;
using System.Web.Configuration;
using System.Text;
using System.Data;
using System.Xml;
using System.Net;
using System.Collections;
using System.Configuration;
using System.Data.SqlClient;
using System.Globalization;


C#
public class testservice : System.Web.Services.WebService
   {
       [WebMethod(EnableSession = true)]
       [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
       public string _testserv(string userid, string password)
       {
           return "Hello World";
       }

       //[WebMethod]
       //public string HelloWorld()
       //{
       //    return "Hello World";
       //}
   }
Posted
Updated 10-May-15 23:26pm
v2

1 solution

Use the browser's dev tools (f12) or Fiddler to examine the network traffic as it will often reveal the actual error. You need to mark your webservice as a script service

C#
[System.Web.Script.Services.ScriptService]
public class testservice : System.Web.Services.WebService
{

    [WebMethod(EnableSession = true)]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string _testserv(string userid, string password)
    {
        return "Hello World";
    }
}


$.ajax
({
    type: "POST",
    url: "testservice.asmx/_testserv",
    data: "{'userid':'" + userid + "','password': '" + txtpwd + "'}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (msg) {
        alert(msg.d);
    },
    error: function (ex) {
        alert(ex.toString());
    }
});
 
Share this answer
 
Comments
rupai99 11-May-15 5:57am    
Thank you very much. I missed the simplest thing.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900