从 Javascript 调用 ASMX Web 服务 [英] Calling ASMX Web Service from Javascript

查看:47
本文介绍了从 Javascript 调用 ASMX Web 服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从 javascript 调用网络服务.

I want to call a webservice from javascript.

这是我的代码:

    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);
    }

我的 ServiceGetStock.asmx 代码:

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;
        }
    }

我收到错误消息:

POST http://www.mywebsite.ro/ServiceGetStock.asmx/GetStock 500(内部服务器错误)

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

[更新]

我忘了提到我在项目的 webconfig(使用 webservice)中添加的,因为我收到错误:

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

XMLHttpRequest 无法加载 http://www.mywebsite.ro/ServiceGetStock.asmx/你好世界.请求的资源上不存在Access-Control-Allow-Origin"标头.因此不允许访问源 'http://localhost:11300'.

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>

推荐答案

好的.我发现了问题.创建 ASMX 文件时,您必须阅读所有注释行.要允许使用 ASP.NET AJAX 从脚本调用此 Web 服务,请取消注释以下行.

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]

所以 GetStock 函数是:

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;
        }
    }

Ajax 代码是:

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

这篇关于从 Javascript 调用 ASMX Web 服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆