0

Hi iam using JSON for the first time in asp.net.

My aspx code :

        <script type="text/javascript">
    $('#imgbtnGo').click(function () {
          alert("Hi");
    var valService = $("#ddlService").val();
          alert("valService"+ valService);
    $.ajax({
        type: "GET",
        url: "/VoyageOneService.svc/BindVoyageDetails?valService =" + valService,
        contentType: "application/json; charset=utf-8", 
        dataType: "Json",
        processdata: true,
        success: function (msg) {
            ServiceSucceeded(msg);
        },
        error: ServiceFailed
    });

   });
   function ServiceSucceeded(result) {
                alert(result);
  }
</script>

And

         <asp:DropDownList ID="ddlService" runat="server" Width="100px" TabIndex="1"></asp:DropDownList>
        <asp:ImageButton ID="imgbtnGo" runat="server" ImageUrl="~/image_repository/go_icon.png"  />

     <asp:ScriptManagerProxy ID="ScriptProxyVoy" runat="server">
       <Services>
        <asp:ServiceReference Path="~/VoyageOneService.svc" />
    </Services>
</asp:ScriptManagerProxy>

..

My Service is :

             public string BindVoyageDetails(int serviceid)
{
                     /// Coding here..
                   Serialization
           MemoryStream stream = new MemoryStream();
    DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(VoyageMaster));
    serializer.WriteObject(stream, objVoyMstr);
    stream.Position = 0;
    StreamReader streamReader = new StreamReader(stream);
    return streamReader.ReadToEnd(); 

}

I hope iam perfect in Service but iam not getting that service fired when i click on my button....

I can not find out the reason can any one please help

3 Answers 3

1

You are using button Id like this

$('#imgbtnGo').click(function () 

butt imgbtnGo is not the actual ID of button after rendering of the page so either you can get it's ClinetID or you need to set ClientIDMode="Static" for the button.

<asp:ImageButton ID="imgbtnGo" runat="server" ImageUrl="~/image_repository/go_icon.png" ClientIDMode="Static"  />
Sign up to request clarification or add additional context in comments.

1 Comment

I have changed it to static mode but still i could not get result. when i check result in alert its NULL, when when i check in service json is returning values
0

Apart from suggested changes in JavaScript as given by Sachin You need to add following attribute to web method (In order to make it emit response in JSON):

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)] 

Source: ASP.NET JSON Web Service Response format

Comments

0

Also you need use the binding binding="webHttpBinding" , go through the below link http://www.codeproject.com/Articles/132809/Calling-WCF-Services-using-jQuery

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.