0

I have a website that contains an email address. I have a webpage that is getting email id. Now I have to send the email id to the web service using ajax. How can I do this? The code below is placed at aspx page.

<script type="text/javascript">
    $(".submit-btn").click(function () {
        var _Email = document.getElementById('txtemail').value;
        alert(_Email);
        $.ajax({
            url: "service/orderit.asmx/SendEmail",
            data: {_email:_Email}
        });
        //PageMethod("/service/orderit.asmx/SendEmail", ['_email', _Email])
    });

The code below is in the web service.

public json_helper.GenericJS SendEmail(string _email)
{
    json_helper.GenericJS retval = new json_helper.GenericJS();

    return retval;
}

This web service is in service folder and the page name is orderit.asmx. Please help.

1 Answer 1

1

If you want webservice can Return JSON

you need add a attribute on your WebService

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string GetJsonContext(){

  // delcare some object 

  Car myCar = new Car();
  myCar.color="RED";


  //Return Json

  return new JavaScriptSerializer().Serialize(myCar)
}

And if you want post JSON to webservice

in webservice , you need create a object that construct is like your json format

like : {name:'Aaron',age:'18',sex:'boy'} . and in your WebService need create a object has name、age、sex property

ex: add a struct

 public struct Person
    {
        public string name { get; set; }
        public string age { get; set; }
        public string sex { get; set; }
    }

and in WebService Method

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string InputJsonContext(Person p){

  //do a some process

   string inputname = p.name;

//etc ...

}

Javascript :

 $.ajax({
                  url: "service/orderit.asmx/InputJsonContext",
                  data: {name:'Aaron',age:'18',sex:'boy'} 
              });
Sign up to request clarification or add additional context in comments.

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.