javascript - Can't Get jquery AJAX request to process -
hi i'm trying simple ajax call method in code behind. @ point i'm testing, i'm keeping simple possible, it's erroring. i'm sure it's simple mistake, have no idea do. seems straight forward, , should work, when click button, pops alert saying "[object object]". here code, appreciated:
aspx file:
<%@ page language="c#" autoeventwireup="true" codebehind="testpage.aspx.cs" inherits="program.testpage" %> <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title></title> <script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script> </head> <body> <form id="form1" runat="server"> <div> <input type="button" id="btnsubmit" value="submit" /> </div> </form> <script type="text/javascript"> $(function () { $('#btnsubmit').click(function () { $.ajax({ type: "post", contenttype: "application/json; charset=utf-8", url: "testpage.aspx/insertdata", datatype: "json", data: "{}", success: function (data) { alert(data.d); }, error: function (result) { alert(result); } }); }); }); </script> </body> </html>
code behind:
using system; using system.collections.generic; using system.linq; using system.web; using system.web.ui; using system.web.ui.webcontrols; using system.text; using system.text.regularexpressions; using system.data.sqlclient; using system.web.services; namespace program { public partial class testpage : system.web.ui.page { protected void page_load(object sender, eventargs e) { } [webmethod] public static string insertdata() { string retmessage = "ddd"; return retmessage; } } }
update
sorry everyone, turns out issue in global.asax. intercepting ajax request, , messing up. once made sure http in asax file, stopped killing ajax request, , started working.
in javascript, you're calling insertdata
post
try
[webmethod] [httppost] public static jsonresult insertdata() { return json(new { message = "ddd" }, jsonrequestbehavior.allowget); }
and in javascript,
success: function (data) { alert(data.message); },
Comments
Post a Comment