简体   繁体   English

从Javascript调用ASMX Web服务

[英]Calling ASMX Web Service from Javascript

I want to call a webservice from javascript. 我想从javascript调用web服务。

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: 我的ServiceGetStock.asmx代码:

 [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) POST http://www.mywebsite.ro/ServiceGetStock.asmx/GetStock 500(内部服务器错误)

[UPDATE] [UPDATE]

I forgot to mention that I added in webconfig of project(with webservice) because I got the error: 我忘了提到我在项目的webconfig中添加了(使用webservice)因为我收到了错误:

XMLHttpRequest cannot load http://www.mywebsite.ro/ServiceGetStock.asmx/HelloWorld . XMLHttpRequest无法加载http://www.mywebsite.ro/ServiceGetStock.asmx/HelloWorld No 'Access-Control-Allow-Origin' header is present on the requested resource. 请求的资源上不存在“Access-Control-Allow-Origin”标头。 Origin 'http:// localhost:11300' is therefore not allowed access. 因此不允许来源“http:// localhost:11300”。

  <httpProtocol>
          <customHeaders>
              <add name="Access-Control-Allow-Origin" value="*" />
              <add name="Access-Control-Allow-Headers" value="Content-Type" />
          </customHeaders>
  </httpProtocol>

Ok guys. 好了朋友们。 I found the problem. 我发现了这个问题。 When an ASMX file is created, you must read all comments lines. 创建ASMX文件时,必须读取所有注释行。 To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 要允许使用ASP.NET AJAX从脚本调用此Web Service,请取消注释以下行。

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

So the GetStock function is: 所以GetStock功能是:

  [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: 和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....... 谢谢大家的提示.......

use dataType: "jsonp", instead of dataType: "json", jsonp is for cross domian webservice. 使用dataType:“jsonp”,而不是dataType:“json”,jsonp用于交叉domian webservice。 hope it will help. 希望它会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM