我无法使以下功能正常工作.这似乎是错误的序列化.这是关于不同数据变体的第5次迭代.我最初只是在做数据:{'id':id}就像我在使用WCF一样,但是使用ASMX它只是不起作用.看起来它将数据序列化为id = 1234而不是id:1234,但我对此很新.任何帮助,将不胜感激.哦,我可以直接在浏览器中调用该服务,它会正确返回数据,所以我知道它不是服务.
function getVentID(id) {
//look up id in database and get VentID
alert('id: ' + id);
var jsdata = { "id": + id}
$.ajax({
type: 'POST',
contentType: 'application/json; charset=utf-8',
url: 'services/UserService.asmx/getVentID',
data: jsdata,
dataType: 'json',
success: function (msg) {
alert(msg.d);
},
error: function (a, b, c) {
alert('Error: ' + a.toString() + ' ' + b.toString() + " " + c.toString());
}
});
}
Run Code Online (Sandbox Code Playgroud)
ps我知道有10个相同的问题,但没有一个我能找到或者对我有用的答案.
我想在我的asmx Web服务中使用用户名和密码验证来实现基本身份验证.
我不想使用WCF,我知道这不是安全的方式,但我需要使用基本身份验证而不使用https.
我的网络服务是这样的:
[WebService(Namespace = "http://www.mywebsite.com/")]
public class Service1
{
[WebMethod]
public string HelloWorld()
{
return "Hello world";
}
}
Run Code Online (Sandbox Code Playgroud)
我使用这个自定义HttpModule:
public class BasicAuthHttpModule : IHttpModule
{
void IHttpModule.Init(HttpApplication context)
{
context.AuthenticateRequest += new EventHandler(OnAuthenticateRequest);
}
void OnAuthenticateRequest(object sender, EventArgs e)
{
string header = HttpContext.Current.Request.Headers["Authorization"];
if (header != null && header.StartsWith("Basic")) //if has header
{
string encodedUserPass = header.Substring(6).Trim(); //remove the "Basic"
Encoding encoding = Encoding.GetEncoding("iso-8859-1");
string userPass = encoding.GetString(Convert.FromBase64String(encodedUserPass));
string[] credentials = userPass.Split(':');
string username …Run Code Online (Sandbox Code Playgroud) 我对Web服务世界很陌生,所以请耐心等待.我正在使用.asmx文件在Visual Studio 2010中创建一个非常简单的Web服务.
这是我正在使用的代码:
namespace MyWebService
{
[WebService(Namespace = "http://www.somedomain.com/webservices")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
public string simpleMethod(String str)
{
return "Hello " + str;
}
}
}
Run Code Online (Sandbox Code Playgroud)
当我调用它并为str参数输入值"John Smith"时,它返回:
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://www.somedomain.com/webservices">Hello John Smith</string>
Run Code Online (Sandbox Code Playgroud)
我的问题是,为Web服务方法返回超过1个值的最佳做法是什么?如果值都是相同的数据类型,我应该使用数组吗?如果值包含不同的数据类型,我需要创建自定义类吗?
要重新创建我所看到的问题,使用VS2010,创建一个空网站并添加一个带有代码隐藏的Web服务(asmx).
使用以下代码,可以成功调用两个web方法:
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class WebService : System.Web.Services.WebService {
[WebMethod]
public void Method1(int x) {
// i'm good
}
[WebMethod]
public string Method2(int x) {
return "it worked";
}
}
Run Code Online (Sandbox Code Playgroud)
现在,如果我将方法2上的parm更改为可空类型它可以正常工作,但它会使方法1失败...
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class WebService : System.Web.Services.WebService {
[WebMethod]
public void Method1(int x) {
// no changes made to this method, but it no longer works
}
[WebMethod]
public string Method2(int? x) {
return "it worked";
}
}
Run Code Online (Sandbox Code Playgroud)
如果在调用服务时缺少参数,则会出现错误:
System.IndexOutOfRangeException:索引超出了数组的范围.在System.Web.Services.Protocols.HttpServer上的System.Web.Services.Protocols.HttpServerType..ctor(Type …
我有一个包含Web服务的ASP.NET Web项目.当我运行该服务时,它会将我带到一个页面,显示所有暴露的方法,使用类似的URL http://api.example.com/game/service.asmx.
在Web Service的代码中,有些方法具有以下属性:
[WebService(Namespace = "http://webservices.example.com/GameServices/Game1")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Game1 : System.Web.Services.WebService
{
// code
}
Run Code Online (Sandbox Code Playgroud)
我对为什么带有webService属性的类上的命名空间与Web服务的路径不同有点困惑.该命名空间来自哪里?它刚刚组成吗?
我只是想知道与REST样式请求兼容的asmx文件?
我有一些asmx文件需要服务一些第三方程序,这些程序设置为发送REST请求,而不是SOAP.
我有以下方法:
using System.Web.Services;
using System.Web.Script.Services;
using System.Web.Script.Serialization;
using Newtonsoft.Json;
using System.Collections;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
// [System.Web.Script.Services.ScriptService]
public class Tripadvisor : System.Web.Services.WebService {
public Tripadvisor () {
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string HotelAvailability(string api)
{
JavaScriptSerializer js = new JavaScriptSerializer();
string json = js.Serialize(api);
//JsonConvert.SerializeObject(api);
return json ;
}
Run Code Online (Sandbox Code Playgroud)
在这里我设置ResponseFormat属性是json仍然作为XML返回.
我想用json格式使用这个asmx服务有什么想法吗?
我看到了类似的问题,但它没有解决我的问题.我在ASMX文件中有一个JSON Web服务;
Web方法的代码
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string GetUserRoles(string JSONUserCode)
{
string retRoles = string.Empty;
List<JSONRole> roles = new List<JSONRole>();
{... I Populate the roles here ...}
DataContractJsonSerializer serializer = new
DataContractJsonSerializer(roles.GetType());
MemoryStream ms = new MemoryStream();
serializer.WriteObject(ms, roles);
string jsonString = Encoding.Default.GetString(ms.ToArray());
ms.Close();
return jsonString;
}
Run Code Online (Sandbox Code Playgroud)
这正确地正确地格式化List,但是用XML包装整个返回.以下是回复:
<?xml version="1.0" encoding="utf-8" ?>
<string xmlns="http://formshare.com/">
[{"Name":"Accounts Payable"},{"Name":"Payroll"}]
</string>
Run Code Online (Sandbox Code Playgroud)
您可以通过单击此链接查看自己的响应:
http://dev.formshare.gologictech.com/JSON/JSONService.asmx/GetUserRoles?JSONUserCode=1234
我需要的回应是:
[{"Name":"Accounts Payable"},{"Name":"Payroll"}]
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?谢谢你的帮助.
我有一个旧的应用程序,它使用经典的Web服务代理与Java Web服务进行交互.不久之后,Web Service托管商决定要求为每个请求发送自定义HTTP标头以访问服务 - 否则请求将被彻底抛弃(看起来这是某种路由器要求).无论我需要在请求中注入自定义HTTP标头的原因是什么.
有没有办法与实际的Http客户端进行交互来执行添加自定义标头的操作?
我正在尝试学习.NET mvc4中的webservices.我尝试创建一个新的Internet应用程序并向项目添加Web服务(asmx).
默认情况下,VS会添加"HelloWorld"Web服务.当我尝试在浏览器中运行它时,我会获得操作列表,服务描述(WSDL)以及HellowWorld操作的详细信息.但是,当我尝试调用webservice时,它会出现以下错误:
'/'应用程序中的服务器错误.
无法找到该资源.
说明:HTTP 404.您要查找的资源(或其中一个依赖项)可能已被删除,名称已更改或暂时不可用.请查看以下网址,确保拼写正确.
我猜可能会遗漏一些基本步骤/设置.请一些人帮忙.谢谢.