0

i am trying to post data from my asp.net webform into database. i have this in my asp.net service (shoppingcart_service.asmx)

 [WebMethod] // 
        public void RegisterSubscriber(string Email)
        {
            new OnlineShopTableAdapters.NewsletterSubscribersTableAdapter().Insert(Email, DateTime.Now);
     //database code


        }

this is my html

  <input type="button" onclick="saveData()" id="btnSave" value="Subscribe"  >

this is my ajax code, which i put in a file called apps.js and linked to my page

//updated ! function saveData() {

 function saveData() {

    var SubscriberEmail = $("#Email").val();
    $.ajax({
        type: "Post",
        url: "shoppingcart_service.asmx/RegisterSubscriber",
        data: '{"Email":"' + SubscriberEmail + '"}',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
            alert(response.status + ' ' + response.statusText);
        },
        error: function (request, status, error) {

        }

    });

}

but the value doesn't get posted to my database consider the error i got in chrome's console, after clicking the subscribe button

error-snapshot

Edwin O.
  • 4,998
  • 41
  • 44
  • you could try to put a breakpoint in the ASP.Net application to confirm if the request is going where you want – bovino Marcelo Bezerra Apr 14 '14 at 15:05
  • Your` dataType & data` you can change to `json` & `data : {email : email}` – Jai Apr 14 '14 at 15:10
  • Try to change: [url: location.pathname + "shoppingcart_service.asmx/RegisterSubscriber"] TO [url: "shoppingcart_service.asmx/RegisterSubscriber"] WITHOUT location.pathname – Sameh Apr 14 '14 at 15:46

4 Answers4

0

I believe the issue is ASMX, it doesn't allow POST on Web Methods. Here's an answer to a similar question Enable ASP.NET ASMX web service for HTTP POST / GET requests Give it a try.

Community
  • 1
  • 1
Roman Mik
  • 3,179
  • 3
  • 27
  • 49
  • @ronmmik, didn't work. i just tried it. i even moved the method to my code behind. didnt still work – Edwin O. Apr 14 '14 at 15:26
  • Does the web method get executed at all? Can you put a break point in it? – Roman Mik Apr 14 '14 at 15:39
  • the web method is working fine. i tested it by requesting it on the browser – Edwin O. Apr 14 '14 at 15:40
  • If you requested in the browser, then it was a GET and not a POST. You will need to use Fiddler (or another tool) to simulate a POST. Can you not run your jquery and watch the break point? Also, I noticed that your datatype: 'jsondata' I'm not sure if that's correct. According to Jquery doc (https://api.jquery.com/jQuery.ajax/) it should be json or jsonp – Roman Mik Apr 14 '14 at 15:44
  • when i changed to GEt this is what i saw in the cosolehttp://localhost/candc/shoppingcart_service.asmx/RegisterSubscriber?{%22Email%22:%22ttttttttttttttttttttttt%22} – Edwin O. Apr 14 '14 at 16:02
  • and when i clicked on that, it opened another browser tab with the error Request format is unrecognized for URL unexpectedly ending in '/RegisterSubscriber'. – Edwin O. Apr 14 '14 at 16:04
  • as per Jai's comment the data is wrong it should be data: {Email: Email} . I would also suggest changing the JavaScript variable name to something else (i.e SubscriberEmail) to prevent confusion. So it should be data: {Email : SubscriberEmail } – Roman Mik Apr 14 '14 at 16:12
0

I have done same thing in one of my projects and it works well..the only difference I can see is that my webmethods return string/bool values and also the webservice class has ScriptService attribute.

See below:

 [System.Web.Script.Services.ScriptService]
 public class ajaxpost : WebService
 {
     [WebMethod]
     public string Callback(string txbFirstname)
     {
     }
 }

and the script side is:

    $.ajax({
        type: "POST",
        url: "/Services/ajaxpost.asmx/Callback",
        data: '{"txbFirstname":"' + txbFirstname + '"}',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(result) {

        },
        error: function(request, status, error) {

        }

    });
Zaki
  • 5,540
  • 7
  • 54
  • 91
0

Change:

url: location.pathname + "shoppingcart_service.asmx/RegisterSubscriber"

TO

url: "shoppingcart_service.asmx/RegisterSubscriber"

WITHOUT location.pathname

Sameh
  • 934
  • 1
  • 14
  • 40
0

Thanks all, i finally found the answer, it lied in the way i feed the webmethod parameter below is the code that worked.

 function saveData2() {
    var SubscriberEmail = $("#Email").val();

    $.ajax({
        type: "POST",
        url: "shoppingcart_service.asmx/RegisterSubscriber",
        data: "email=" + SubscriberEmail, // the data in form-encoded format, ie as it would appear on a querystring
        //contentType: "application/x-www-form-urlencoded; charset=UTF-8", // if you are using form encoding, this is default so you don't need to supply it
        dataType: "text", // the data type we want back, so text.  The data will come wrapped in xml
        success: function (data) {
            $("#searchresultsA").html(data); // show the string that was returned, this will be the data inside the xml wrapper
        }
    });
    }

i didnt need to use curly braces in the data line

Edwin O.
  • 4,998
  • 41
  • 44