从 Javascript 调用 ASMX Web 服务

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

我想从 javascript 调用网络服务.

I want to call a webservice from javascript.

这是我的代码:

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

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

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

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>

推荐答案

好的.我发现了问题.创建 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 = "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:46:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1469181.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Javascript   ASMX   Web

发布评论

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

>www.elefans.com

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