Click here to Skip to main content
15,566,114 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Friends,

i have this sample code in jQuery that connect to my WebService

sending text, and receive text. its works excellent.

JavaScript
<html  lang="en-US">
<head>
<meta http-equiv="content-type" content="application/xhtml+xml; charset=UTF-8" />
        <meta name="viewport" content="width=device-width" />
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
        <script type="text/javascript">

            //var WS_URI = 'http://10.0.0.5/WS_Catalog_SQL/Service1.asmx/GetItem';
            var WS_URI = 'http://10.0.0.5/WS_Catalog/Service1.asmx/GetItem';
            var MAKAT ;

            function Look() {
                MAKAT = document.getElementById("txt").value.toString();
                $.ajax({
                    ServiceCallID: 1,
                    url: WS_URI,
                    type: 'POST',
                    data: '{"Makat": "' + MAKAT + '"}',
                    contentType: 'application/json; charset=utf-8',
                    dataType: 'json',
                    success:
                        function (data, textStatus, XMLHttpRequest) {
                            //document.writeln(data.d);
                            $("#RES").text((data.d).toString());
                        },
                    error:
                  function (XMLHttpRequest, textStatus, errorThrown) {
                      alert(textStatus);
                  }
                });
            }
    </script>
</head>

<body >
          <br />
          <button onclick="Look();">Press me</button>
          <input id="txt"  value="1111"/>
          <br />
          <p id="RES">res</p>
</body>
</html>


now i need to receive DataSet (xml),

i build WebService (in C#) that receive text and sending DataSet

but the jQuery dosent work.

I broke my head for two days and can not seem to find a solution....
Posted

On Service side pls check
1. u have to add
[System.Web.Script.Services.ScriptService]

before web service class to enable service access from script
2. add this before web service method
[ScriptMethod(ResponseFormat = ResponseFormat.Xml)]

to specify the out put format
3. better return Data as xml instead of DataSet

On Client side

$.ajax({
         url: WS_URI,
         type: 'POST',
         data: "Makat="+ MAKAT,
         dataType: "xml",
         success:
           function (data) {
             var item=$(data).find('Item').text();
             var des=$(data).find('Des').text();
           },
         error:
           function (errorThrown) {
            alert(errorThrown);
            }
        });
 
Share this answer
 
Comments
goldsoft 18-Jul-14 6:45am    
thanks !!!!!! its work !!!!!!!
change datatype as
dataType: "xml",
 
Share this answer
 
Comments
goldsoft 18-Jul-14 3:17am    
thanks for the help, i try this but i got error.
my WebService code:
[WebMethod]
public DataSet GetItem(string Makat)
{
dsView = new DataSet();
OpenConnection();
SQL = "select Item,Des from MyTbl where Item = '" + Makat + "'";
dsView = new DataSet();
adp_SQL = new SqlDataAdapter(SQL, Conn_SQL);
adp_SQL.Fill(dsView, "MyTbl ");
adp_SQL.Dispose();
if (dsView.Tables["MyTbl "].Rows.Count >= 1)
{
return dsView;
}
else
{
return null;
}
}
goldsoft 18-Jul-14 3:41am    
i try this:

contentType: "text/xml",
dataType: "text",

and got this error:

POST http://10.0.0.5/WS_Catalog_SQL/Service1.asmx/GetItem 500 (Internal Server Error) jquery.min.js:130
c.extend.ajax jquery.min.js:130
Look (index):14
onclick

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