JSON ajax WebMethod Callhttp://forums.asp.net/t/1784905.aspx/1?JSON+ajax+WebMethod+CallMon, 26 Mar 2012 03:09:25 -040017849054897977http://forums.asp.net/p/1784905/4897977.aspx/1?JSON+ajax+WebMethod+CallJSON ajax WebMethod Call <p>i'm trying to call my webservice for adding an entry into my database. the webmethod only takes 1 parameter, and doesn't return any data. i set a breakpoint on my webservice and it's not hitting it, nor is the alert showing up. all of the other alerts and validation is working on the javascript function, so i know it's the webservice call. i'm fairly new to webservices and JSON, and obviously i'm doing something wrong. any help would be appreciate. thanks in advance</p> <p>// webservice</p> <pre class="prettyprint">using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Services; using DataWarehouse; using System.Web.Script.Services; namespace DesignAndAlign { /// &lt;summary&gt; /// Summary description for AddNewsLetter /// &lt;/summary&gt; [WebService(Namespace = &quot;http://tempuri.org/&quot;)] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. [ScriptService] public class AddNewsLetter : System.Web.Services.WebService { [WebMethod] public static void AddMonthlyNewsletter(string emailAddress) { using (DesignAndAlignEntities context = new DesignAndAlignEntities()) { NewsLetter news = new NewsLetter() { SubscriberEmail = emailAddress }; context.NewsLetters.AddObject(news); context.SaveChanges(); } } } }</pre> <p>// javascript</p> <pre class="prettyprint"> &#36;('.LinkButtonSubscribeEmail').click(function () { var value = &#36;('.SubscribeTextBox').val(); var subscribeError = ""; var subscribeFlag = false; if (value === "") { subscribeError = "Please enter an email address for your news subscription."; subscribeFlag = true; } else { if (reg.test(value) == false) { subscribeError = "Please enter a valid email address."; subscribeFlag = true; } } if (subscribeFlag) { &#36;('#dialog-message-Error').html('&lt;div style="padding:5px;"&gt;&lt;span style="line-height:18px;"&gt;' + subscribeError + '&lt;/span&gt;&lt;/div&gt;').dialog( { modal: true, draggable: false, resizable: false, height: 'auto', show: 'slide', buttons: { Ok: function () { &#36;(this).dialog('close'); } } }); return false; } if (!subscribeFlag) { // &#36;('#dialog-message-Error').html('&lt;div style="padding:5px;"&gt;&lt;span style="line-height:18px;"&gt;You have been added to the monthly newsletter.&lt;br/&gt;Thank you!&lt;/span&gt;&lt;/div&gt;').dialog( // { modal: true, draggable: false, resizable: false, height: 'auto', show: 'slide', buttons: { Ok: function () { &#36;(this).dialog('close'); } } }); &#36;.ajax({ type: "POST", url: "AddNewsLetter.asmx/AddMonthlyNewsletter", data: "{'emailAddress':'" + value + "'}", contentType: "application/json; charset=utf-8", dataType: "json", success: function (msg) { alert('woohoo'); } }); &#36;('.SubscribeTextBox').val(''); return false; } }); </pre> <p>// masterpage</p> <pre class="prettyprint">&lt;body&gt; &lt;form id="form1" runat="server"&gt; &lt;asp:ScriptManager ID="ScriptManager1" runat="server"&gt; &lt;Services&gt; &lt;asp:ServiceReference Path="~/AddNewsLetter.asmx" /&gt; &lt;/Services&gt; &lt;/asp:ScriptManager&gt; &lt;div style="margin-left: 30px; margin-top: 5px; width: 300px; float: left;"&gt; &lt;div style="float: left; width: 130px;"&gt; &lt;input type="button" id="HeaderSubscribeButton" class="HeaderSubscribeButton" /&gt; &lt;div class="SubscribeForm"&gt; &lt;div class="CloseSubscribeFormButton"&gt; &lt;/div&gt; &lt;div style="margin-top: 86px; margin-left: 70px;"&gt; &lt;asp:TextBox ID="TextBoxSubscribeEmail" runat="server" class="SubscribeTextBox" /&gt; &lt;div style="padding-top: 44px; float: right; padding-right: 50px;"&gt; &lt;asp:LinkButton ID="LinkButtonSubscribeEmail" runat="server" Width="100px" Height="25px" class="LinkButtonSubscribeEmail" /&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; </pre> <p></p> 2012-03-25T14:45:30-04:004898058http://forums.asp.net/p/1784905/4898058.aspx/1?Re+JSON+ajax+WebMethod+CallRe: JSON ajax WebMethod Call <p>As a first approach to solving the issue, I would recommend you to explore the possible error message from the request to be able to find out additional information about the problem.</p> <p>var request = &#36;.ajax({ <br> &nbsp; url: &quot;AddNewsLetter.asmx/AddMonthlyNewsletter&quot;, <br> &nbsp; type: &quot;POST&quot;, <br> &nbsp; data: &quot;{'emailAddress':'&quot; &#43; value &#43; &quot;'}&quot;, <br> &nbsp; contentType: &quot;application/json; charset=utf-8&quot;, <br> &nbsp; dataType: &quot;json&quot;</p> <p>}); <br> &nbsp;<br> request.done(function(msg) { <br> &nbsp; alert(&quot;success&quot;);<br> });&nbsp;<br> &nbsp;<br> <strong>request.fail(function(jqXHR, textStatus) { </strong><br> <strong>&nbsp; alert( &quot;Request failed: &quot; &#43; textStatus ); </strong><br> <strong>});</strong></p> 2012-03-25T16:30:10-04:004898203http://forums.asp.net/p/1784905/4898203.aspx/1?Re+JSON+ajax+WebMethod+CallRe: JSON ajax WebMethod Call <p>all it says is:</p> <p>Request failed: error</p> <p>Update:&nbsp;</p> <p>i added this</p> <p>request.fail(function (jqXHR, textStatus, errorThrown) {<br> alert(&quot;Request failed: &quot; &#43; textStatus, errorThrown);&nbsp;</p> <p></p> <p>and it gave me:</p> <p>Request failed: error Internal Server Error</p> <p>is there anything else i can put in there to give more detailed information?</p> <p>i hovered over the &quot;jqXHR&quot; and it says:&nbsp;</p> <p>readyState: 4 &nbsp; &nbsp; status: 500</p> 2012-03-25T19:33:56-04:004898220http://forums.asp.net/p/1784905/4898220.aspx/1?Re+JSON+ajax+WebMethod+CallRe: JSON ajax WebMethod Call <p>hello</p> <p>try this service:</p> <pre class="prettyprint">using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Services; using DataWarehouse; using System.Web.Script.Services; namespace DesignAndAlign { /// &lt;summary&gt; /// Summary description for AddNewsLetter /// &lt;/summary&gt; [WebService(Namespace = &quot;http://tempuri.org/&quot;)] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. [ScriptService] public class AddNewsLetter : System.Web.Services.WebService { [WebMethod, System.Web.Script.Services.ScriptMethod] public static void AddMonthlyNewsletter(string emailAddress) { using (DesignAndAlignEntities context = new DesignAndAlignEntities()) { NewsLetter news = new NewsLetter() { SubscriberEmail = emailAddress }; context.NewsLetters.AddObject(news); context.SaveChanges(); } } } }</pre> 2012-03-25T19:45:35-04:004898226http://forums.asp.net/p/1784905/4898226.aspx/1?Re+JSON+ajax+WebMethod+CallRe: JSON ajax WebMethod Call <p>i tried that earlier:</p> <p>Error 11 Attribute 'System.Web.Script.Services.ScriptService' is not valid on this declaration type. It is only valid on 'class, interface' declarations. C:\Users\Rob\Documents\Visual Studio 2010\Projects\DesignAndAlign\DesignAndAlign\AddNewsLetter.asmx.cs 22 21 DesignAndAlign</p> <p>thanks though</p> 2012-03-25T19:49:59-04:004898237http://forums.asp.net/p/1784905/4898237.aspx/1?Re+JSON+ajax+WebMethod+CallRe: JSON ajax WebMethod Call <p>this was in the jqXHR.responseText</p> <pre class="prettyprint">&lt;html&gt; &lt;head&gt; &lt;title&gt;Unknown web method AddMonthlyNewsletter.&lt;br&gt;Parameter name: methodName&lt;/title&gt; &lt;style&gt; body {font-family:&quot;Verdana&quot;;font-weight:normal;font-size: .7em;color:black;} p {font-family:&quot;Verdana&quot;;font-weight:normal;color:black;margin-top: -5px} b {font-family:&quot;Verdana&quot;;font-weight:bold;color:black;margin-top: -5px} H1 { font-family:&quot;Verdana&quot;;font-weight:normal;font-size:18pt;color:red } H2 { font-family:&quot;Verdana&quot;;font-weight:normal;font-size:14pt;color:maroon } pre {font-family:&quot;Lucida Console&quot;;font-size: .9em} .marker {font-weight: bold; color: black;text-decoration: none;} .version {color: gray;} .error {margin-bottom: 10px;} .expandable { text-decoration:underline; font-weight:bold; color:navy; cursor:hand; } &lt;/style&gt; &lt;/head&gt; &lt;body bgcolor=&quot;white&quot;&gt; &lt;span&gt;&lt;H1&gt;Server Error in '/' Application.&lt;hr width=100% size=1 color=silver&gt;&lt;/H1&gt; &lt;h2&gt; &lt;i&gt;Unknown web method AddMonthlyNewsletter.&lt;br&gt;Parameter name: methodName&lt;/i&gt; &lt;/h2&gt;&lt;/span&gt; &lt;font face=&quot;Arial, Helvetica, Geneva, SunSans-Regular, sans-serif &quot;&gt; &lt;b&gt; Description: &lt;/b&gt;An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. &lt;br&gt;&lt;br&gt; &lt;b&gt; Exception Details: &lt;/b&gt;System.ArgumentException: Unknown web method AddMonthlyNewsletter.&lt;br&gt;Parameter name: methodName&lt;br&gt;&lt;br&gt; &lt;b&gt;Source Error:&lt;/b&gt; &lt;br&gt;&lt;br&gt; &lt;table width=100% bgcolor=&quot;#ffffcc&quot;&gt; &lt;tr&gt; &lt;td&gt; &lt;code&gt; An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.&lt;/code&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;br&gt; &lt;b&gt;Stack Trace:&lt;/b&gt; &lt;br&gt;&lt;br&gt; &lt;table width=100% bgcolor=&quot;#ffffcc&quot;&gt; &lt;tr&gt; &lt;td&gt; &lt;code&gt;&lt;pre&gt; [ArgumentException: Unknown web method AddMonthlyNewsletter. Parameter name: methodName] System.Web.Script.Services.WebServiceData.GetMethodData(String methodName) &#43;539954 System.Web.Script.Services.RestHandler.CreateHandler(WebServiceData webServiceData, String methodName) &#43;10 System.Web.Script.Services.RestHandler.CreateHandler(HttpContext context) &#43;159 System.Web.Script.Services.RestHandlerFactory.GetHandler(HttpContext context, String requestType, String url, String pathTranslated) &#43;62 System.Web.Script.Services.ScriptHandlerFactory.GetHandler(HttpContext context, String requestType, String url, String pathTranslated) &#43;47 System.Web.HttpApplication.MapHttpHandler(HttpContext context, String requestType, VirtualPath path, String pathTranslated, Boolean useAppConfig) &#43;203 System.Web.MapHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() &#43;128 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&amp;amp; completedSynchronously) &#43;184 &lt;/pre&gt;&lt;/code&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;br&gt; &lt;hr width=100% size=1 color=silver&gt; &lt;b&gt;Version Information:&lt;/b&gt;&amp;nbsp;Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.272 &lt;/font&gt; &lt;/body&gt; &lt;/html&gt; &lt;!-- [ArgumentException]: Unknown web method AddMonthlyNewsletter. Parameter name: methodName at System.Web.Script.Services.WebServiceData.GetMethodData(String methodName) at System.Web.Script.Services.RestHandler.CreateHandler(WebServiceData webServiceData, String methodName) at System.Web.Script.Services.RestHandler.CreateHandler(HttpContext context) at System.Web.Script.Services.RestHandlerFactory.GetHandler(HttpContext context, String requestType, String url, String pathTranslated) at System.Web.Script.Services.ScriptHandlerFactory.GetHandler(HttpContext context, String requestType, String url, String pathTranslated) at System.Web.HttpApplication.MapHttpHandler(HttpContext context, String requestType, VirtualPath path, String pathTranslated, Boolean useAppConfig) at System.Web.HttpApplication.MapHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&amp; completedSynchronously) --&gt;</pre> 2012-03-25T19:55:01-04:004898238http://forums.asp.net/p/1784905/4898238.aspx/1?Re+JSON+ajax+WebMethod+CallRe: JSON ajax WebMethod Call <p>Try this for tracing:</p> <p><strong>request.fail(function(xmlRequest, textStatus) {&nbsp;</strong><br> <strong>alert(xmlRequest.status &#43; ' \n\r ' &#43; xmlRequest.statusText &#43; '\n\r' &#43; xmlRequest.responseText);&nbsp; <br> </strong><strong>});</strong></p> 2012-03-25T19:55:30-04:004898241http://forums.asp.net/p/1784905/4898241.aspx/1?Re+JSON+ajax+WebMethod+CallRe: JSON ajax WebMethod Call <p>that was a spelling issue i changed that to ScriptMethod.</p> 2012-03-25T19:56:31-04:004898245http://forums.asp.net/p/1784905/4898245.aspx/1?Re+JSON+ajax+WebMethod+CallRe: JSON ajax WebMethod Call <p>The method will only need the [WebMethod] attribute. Try remove the static keyword.</p> <p><span class="kwd">[WebMethod]<br> public</span><span class="pln"> </span><span style="text-decoration:line-through"><span class="kwd">static</span></span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> </span><span class="typ">AddMonthlyNewsletter</span><span class="pun">(</span><span class="kwd">string</span><span class="pln"> emailAddress</span><span class="pun">)</span></p> 2012-03-25T19:58:20-04:004898252http://forums.asp.net/p/1784905/4898252.aspx/1?Re+JSON+ajax+WebMethod+CallRe: JSON ajax WebMethod Call <p>thank you very much btw.</p> <p>but i'm confused. why is it that EVERYWHERE i read they said &quot;WebMethods need to <strong>always</strong> be static&quot;, yet here it doesn't need to be. could you elaborate?</p> 2012-03-25T20:08:53-04:004898417http://forums.asp.net/p/1784905/4898417.aspx/1?Re+JSON+ajax+WebMethod+CallRe: JSON ajax WebMethod Call <p>Hi robwscott,</p> <p>For &quot;WebMethod&quot; defined in a standard ASP.NET ASMX webservice, you do not need to mark the function as &quot;Static&quot; one. <br> <br> However, ASP.NET also supports creating &quot;WebMethod&quot; in ASPX web page class. In such cases, the &quot;WebMethod&quot; you define in ASPX page class need to be &quot;Static&quot; member function. Therefore, I thinkk the reference/articles you saw which said &quot;WebMethods&quot; need to always be static should be targeting the &quot;WebMethod&quot; defined in ASPX web page. Here are some refrences mentioned both approaches:</p> <p>#Exposing Web Services to Client Script <br> <a href="http://msdn.microsoft.com/en-us/library/bb398998.aspx">http://msdn.microsoft.com/en-us/library/bb398998.aspx</a></p> <p>#Using Page Methods in ASP.NET AJAX <br> <a href="http://www.singingeels.com/Articles/Using_Page_Methods_in_ASPNET_AJAX.aspx">http://www.singingeels.com/Articles/Using_Page_Methods_in_ASPNET_AJAX.aspx</a></p> 2012-03-26T03:09:25-04:00