Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
654 views
in Technique[技术] by (71.8m points)

jquery - Calling ASMX Web Service from Javascript

I want to call a webservice from javascript.

This is my code:

    var method="GetStock";
    var url = "http://www.mywebsite.ro/ServiceGetStock.asmx";
    $.ajax({
        type: "POST",
        url: url + "/GetStock",
        data: "{variant_id='1'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: OnSuccessCall,
        error: OnErrorCall
    });

    function OnSuccessCall(response) {
        alert(response.d);
    }


    function OnErrorCall(response) {
        alert(response.status + " " + response.statusText);
    }

My ServiceGetStock.asmx code:

 [WebMethod]
    public string GetStock(int variant_id)
    {
        try
        {

            ProductVariant variant = ProductVariantManager.GetProductVariantByID(variant_id);

            return variant.Stock.ToString();
        }
        catch (Exception ex)
        {
            return ex.Message;
        }
    }

I got the error message:

POST http://www.mywebsite.ro/ServiceGetStock.asmx/GetStock 500 (Internal Server Error)

[UPDATE]

I forgot to mention that I added in webconfig of project(with webservice) because I got the error:

XMLHttpRequest cannot load http://www.mywebsite.ro/ServiceGetStock.asmx/HelloWorld. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http:// localhost:11300' is therefore not allowed access.

  <httpProtocol>
          <customHeaders>
              <add name="Access-Control-Allow-Origin" value="*" />
              <add name="Access-Control-Allow-Headers" value="Content-Type" />
          </customHeaders>
  </httpProtocol>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Ok guys. I found the problem. When an ASMX file is created, you must read all comments lines. To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.

 //[System.Web.Script.Services.ScriptService]

So the GetStock function is:

  [WebMethod]
     [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string GetStock(string variant_id)
    {
        SendEmail.SendErrorMail("in"+ variant_id);

        try
        {

            ProductVariant variant = ProductVariantManager.GetProductVariantByID(Convert.ToInt32(variant_id));

            return variant.Stock.ToString();
        }
        catch (Exception ex)
        {
            return ex.Message;
        }
    }

and the Ajax code is:

   var url = "http://www.mywebsite.ro/ServiceGetStock.asmx";
    $.ajax({
        type: "POST",
        url: url + "/GetStock",
        data: "{variant_id:'1'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: OnSuccessCall,
        error: OnErrorCall
    });

    function OnSuccessCall(response) {
        alert(response.d);
    }


    function OnErrorCall(response) {
        alert(response.status + " " + response.statusText);
    }

Problem solved! Thanks all for tips.......


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...