简体   繁体   English

JSON-asp.net中的ButtonClick无法启动?

[英]JSON - ButtonClick in asp.net is not firing?

Hi iam using JSON for the first time in asp.net. 嗨,iam在asp.net中首次使用JSON。

My aspx code : 我的aspx代码:

        <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.... 我希望Iam在服务方面是完美的,但是当我单击按钮时,我不会触发该服务。

I can not find out the reason can any one please help 我找不到原因可以帮助任何人的原因

You are using button Id like this 您正在使用这样的按钮ID

$('#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. imgbtnGo不是呈现页面后按钮的实际ID,因此您可以获取它的ClinetID或需要为按钮设置ClientIDMode="Static"

<asp:ImageButton ID="imgbtnGo" runat="server" ImageUrl="~/image_repository/go_icon.png" ClientIDMode="Static"  />

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): 除了Sachin给JavaScript提出的建议更改之外,您还需要向Web方法添加以下属性(以使其在JSON中发出响应):

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

Source: ASP.NET JSON Web Service Response format 来源: ASP.NET JSON Web服务响应格式

另外,您还需要使用binding binding="webHttpBinding" ,通过下面的链接http://www.codeproject.com/Articles/132809/Calling-WCF-Services-using-jQuery

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM