最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Calling ASMX Web Service from Javascript - Stack Overflow

programmeradmin3浏览0评论

I want to call a webservice from javascript.

This is my code:

    var method="GetStock";
    var url = ".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 .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 .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>

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>
Share Improve this question edited Jan 22, 2014 at 15:42 POIR asked Jan 22, 2014 at 14:38 POIRPOIR 3,1909 gold badges34 silver badges48 bronze badges 13
  • What is the method signature for your GetStock – Habib Commented Jan 22, 2014 at 14:39
  • Use Fiddler (fiddler2.com) to emulate the POST request (using the Composer tab) and take a look at your response - this will tell you specifically the detailed error message you're actually receiving. – Mike Marks Commented Jan 22, 2014 at 14:41
  • 500 is a server-side error. Have you debugged the server-side code? Also, "{id='1'}" isn't valid json, and id != variant_id. I'd imagine that matters. – Jason P Commented Jan 22, 2014 at 14:42
  • Try {"variant_id":"1"} – Mike Marks Commented Jan 22, 2014 at 14:42
  • Ok. I changed id into variant_id. It was my mistake. But I got the same error. – POIR Commented Jan 22, 2014 at 14:44
 |  Show 8 more comments

2 Answers 2

Reset to default 15

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.......

use dataType: "jsonp", instead of dataType: "json", jsonp is for cross domian webservice. hope it will help.

发布评论

评论列表(0)

  1. 暂无评论