我想从JavaScript调用web服务。
这是我的code:
VAR方法=GetStock; VAR URL =http://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 =http://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服务
发布评论