从JavaScript调用ASMX Web服务

编程入门 行业动态 更新时间:2024-10-26 10:35:41
本文介绍了从JavaScript调用ASMX Web服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想从JavaScript调用web服务。

这是我的code:

VAR方法=GetStock;    VAR URL =htt​​p://www.mywebsite.ro/ServiceGetStock.asmx;    $阿贾克斯({        键入:POST,        URL:+/ GetStock        数据:{variant_id ='1'},        的contentType:应用/ JSON的;字符集= UTF-8,        数据类型:JSON        成功:OnSuccessCall,        错误:OnErrorCall    });    功能OnSuccessCall(响应){        警报(response.d);    }    功能OnErrorCall(响应){        警报(response.status ++ response.statusText);    }

我ServiceGetStock.asmx code:

[的WebMethod]    公共字符串GetStock(INT variant_id)    {        尝试        {            ProductVariant变种= ProductVariantManager.GetProductVariantByID(variant_id);            返回variant.Stock.ToString();        }        赶上(异常前)        {            返回ex.Message;        }    }

我得到的错误消息:

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

[更新]

我忘了提,我在项目(与Web服务)的webconfig添加,因为我得到了错误:

的 XMLHtt prequest无法加载 www.mywebsite.ro/ ServiceGetStock.asmx /的HelloWorld 。无访问控制允许来源标头的请求的资源present。原产地的http://本地主机:11300'。因此不允许访问的

< httpProtocol>          < customHeaders>              <添加名称=访问控制允许来源VALUE =*/>              <添加名称=访问控制 - 允许 - 头VALUE =Content-Type的/>          < / customHeaders>  < / httpProtocol>

解决方案

玉家伙。我发现这个问题。当创建一个ASMX文件中,请您务必阅读注释行。要允许此Web服务从脚本调用,使用ASP.NET AJAX,取消注释以下行。

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

所以GetStock功能是:

[的WebMethod]     [ScriptMethod(ResponseFormat = ResponseFormat.Json)    公共字符串GetStock(字符串variant_id)    {        SendEmail.SendErrorMail(在+ variant_id);        尝试        {            ProductVariant变种= ProductVariantManager.GetProductVariantByID(Convert.ToInt32(variant_id));            返回variant.Stock.ToString();        }        赶上(异常前)        {            返回ex.Message;        }    }

和阿贾克斯code是:

VAR URL =htt​​p://www.mywebsite.ro/ServiceGetStock.asmx;    $阿贾克斯({        键入:POST,        URL:+/ GetStock        数据:{variant_id:'1'},        的contentType:应用/ JSON的;字符集= UTF-8,        数据类型:JSON        成功:OnSuccessCall,        错误:OnErrorCall    });    功能OnSuccessCall(响应){        警报(response.d);    }    功能OnErrorCall(响应){        警报(response.status ++ response.statusText);    }

问题解决了!感谢所有的提示.......

I want to call a webservice from javascript.

This is my code:

var method="GetStock"; var url = "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 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 www.mywebsite.ro/ServiceGetStock.asmx/HelloWorld. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin ' 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>

解决方案

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 = "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服务

本文发布于:2023-10-07 10:45:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1469180.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:JavaScript   ASMX   Web

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!