我一直在阅读当你想要返回XML时每个人都声明如何返回XmlDocument.有没有办法将原始XML作为字符串返回?我使用了许多返回包含XML的字符串的Web服务(由其他人编写).如果您返回XmlDocument,那么不在.Net上的用户如何使用该方法?
将原始XML作为字符串返回而没有包装的方法是<string></string>什么?
谢谢!
我完全被困惑了.我有一个我想通过javascript调用的WebMethod.很简单,使用jQuery,我之前已经完成了这个.然而这一次,我收到一个错误,指出我没有提供WebMethod的论据.但在我看来,我是.
我在我的aspx代码隐藏中有这个:
<WebMethod(True)> _
<ScriptMethod(UseHttpGet:=True)> _
Public Shared Function GetTimes(ByVal input as String) As Object()
Dim result As New List(Of Object)
result.Add(New With {.Text = "5:30", .Value = "1"})
result.Add(New With {.Text = "6:00", .Value = "2"})
result.Add(New With {.Text = "6:30", .Value = "3"})
result.Add(New With {.Text = "7:00", .Value = "4"})
Return result.ToArray
End Function
Run Code Online (Sandbox Code Playgroud)
这在我的javascript中:
$.ajax({
url: 'ThePage.aspx/GetTimes',
contentType: "application/json; charset=utf-8",
data: '{"input":"test"}',
dataType: 'json',
succes: function (result) {
alert('yep');
},
error: function (request, errorType, obj) { …Run Code Online (Sandbox Code Playgroud) 我只需要了解一下.net中的Web服务(.asmx和所有东西)
对于我的项目,我需要安排一个程序,该程序会对一组网站进行calwels,然后每隔30分钟将新的可用数据更新到数据库.
我创建了一个[WebMethod]for crawler代码来运行和数据库更新(如果可用).
但是我希望这[WebMethod]可以自动化并且每30分钟运行一次?
碰壁了,在文档中找不到太多内容。
我有两本词典,我想把它们放在一个列表中。
Dim listOfDictionaries As List(Of Dictionary(Of String, String))
Run Code Online (Sandbox Code Playgroud)
不管用。
我是否正确地假设一旦我变暗,我就可以添加传统的方式?
详细信息(编辑)
当尝试 listOfDictionaries.Add(dictionaryIWantToAdd) 时,我得到“‘一维数组 system.collection.generic.dictionary(of string, string)’类型的值无法转换为‘system.collection.generic.dictionary(of string) , 细绳)'
解决方案
有助于将 () 放在数组末尾。:P
我有一个webmethod接收包含日期属性的对象.我传递了一个代表webmethod对象的json.asp.net自动将json字符串转换为对象,因此我的方法如下所示:
[WebMethod]
public static Object Save(MyObject item)
{
....
}
public class MyObject
{
DateTime date;
.....
}
Run Code Online (Sandbox Code Playgroud)
当以美国格式发送日期时,这一切都很有效:'MM/dd/yyyy'
当日期以欧洲格式发送时:'dd/MM/yyy'我收到错误.
我在全局asax中设置线程的文化:
void Application_AcquireRequestState(object sender, EventArgs e)
{
var s = GetSessionValues();
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(s.CultureID);
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(s.CultureID);
}
Run Code Online (Sandbox Code Playgroud)
此代码正确运行,它从会话中读取文化ID并设置它.从数据库登录时,将文化ID读入会话.
但这似乎对我收到错误的Web方法所期望的日期格式没有影响:
"31/08/2012 is not a valid value for DateTime.",
"StackTrace":"
at System.ComponentModel.DateTimeConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)\r\n
at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject)\r\n
at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeMain(Object o, Type type, JavaScriptSerializer serializer, Boolean …Run Code Online (Sandbox Code Playgroud) 在这里,我从Jquery Ajax调用webmethod.在成功函数中,我看到有一个错误称为"身份验证失败"
在这里,我有错误的图像

我的WebMethod
[WebMethod,ScriptMethod]
public static List<UploadedFiles> GetAllUploadedFiles()
{
List<UploadedFiles> UploadedFilesDetails = new List<UploadedFiles>();
try
{
SqlCommand comGetAllFiles = new SqlCommand("SP_GetAllUploadedFiles", conDB);
comGetAllFiles.CommandType = CommandType.StoredProcedure;
if (conDB.State == ConnectionState.Closed)
conDB.Open();
SqlDataReader rdr = comGetAllFiles.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(rdr);
foreach (DataRow r in dt.Rows)
{
UploadedFilesDetails.Add(new UploadedFiles
{
Id = (int)r["Id"],
UserId =(Guid)r["UserId"],
FilePath = r["FilePath"].ToString(),
Date =(DateTime) r["Date"]
});
}
}
catch(Exception ee)
{
}
finally
{
conDB.Close();
}
return UploadedFilesDetails;
}
Run Code Online (Sandbox Code Playgroud)
我的Ajax功能
<script>
$(function () { …Run Code Online (Sandbox Code Playgroud) 当从 ajax 调用一个 WebMethod 时,如果我返回一个简单的字符串,ajax 代码将转到成功方法而不会出错,如下所示:
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static string Hello()
{
return "Hello World";
}
Run Code Online (Sandbox Code Playgroud)
这是正确的,但是,如何使 WebMethod 返回错误的正确方法是在 ajax 错误方法而不是成功方法中捕获该错误?
谢谢
我正在使用summernote,我想将图像上传到我的网络服务器..下面是我正在使用的代码
默认.aspx
<script type="text/javascript">
$(document).ready(function () {
$('#summernote').summernote({
onImageUpload: function (files, editor, $editable) {
alert('image?');
var formData = new FormData();
formData.append("file", files[0]);
$.ajax({
url: "Default.aspx/UploadImage",
data: formData,
type: 'POST',
cache: false,
contentType: false,
processData: false,
success: function (imageUrl) {
alert(imageUrl);
if (!imageUrl) {
// handle error
return;
}
editor.insertImage($editable, imageUrl);
},
error: function () {
alert('error');
// handle error
}
});
console.log('image upload:', files, editor, $editable);
}
});
});
</script>
Run Code Online (Sandbox Code Playgroud)
默认.asp.cs
[System.Web.Services.WebMethod]
public static string UploadImage(HttpPostedFileBase file)
{
//Saving to …Run Code Online (Sandbox Code Playgroud) 我有一个会话变量,我需要在具有许多webmethod函数的cs页面上使用.如果我声明如下,我并不总是得到最新的变量.有时它会给我在最后一个之前存储的变量.我究竟做错了什么?
public partial class uc_functions : MyBasePage
{
static string ucService = HttpContext.Current.Session["ucService"] as string;
....
[WebMethod] //1
[WebMethod] //2
[WebMethod] //3
Run Code Online (Sandbox Code Playgroud) 我正在使用以下方法从我的ASP.NET WebMethod向我的Javascript发送一个List:
List<Person> plist = new List<Person>();
string json = "";
System.Web.Script.Serialization.JavaScriptSerializer oSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
json = oSerializer.Serialize(plist );
return json;
Run Code Online (Sandbox Code Playgroud)
我正在使用它从我的JS中检索这些值:
function onSuccess(val) {
var obj = JSON.parse(val)
obj.forEach(function (entry) {
console.log(entry);
});
}
Run Code Online (Sandbox Code Playgroud)
一切都很好.但是现在我想返回MULTIPLE列表,例如:
List<Person> plist = new List<Person>();
List<Car> carlist = new List<Car>();
List<Car> jeeplist = new List<Car>();
Run Code Online (Sandbox Code Playgroud)
使用我之前使用的相同机制.如何将两个列表插入到Serializer中并从我的JS中打印出来?
编辑
我如何从JS获取值?这是这样的吗?
obj.forEach(function (entry) {
entry.forEach(function (act)
{
console.log(act);
})
});
Run Code Online (Sandbox Code Playgroud) webmethod ×10
asp.net ×7
c# ×6
ajax ×3
jquery ×3
json ×2
web-services ×2
.net ×1
asmx ×1
code-behind ×1
date-format ×1
dictionary ×1
javascript ×1
list ×1
vb.net ×1
webforms ×1
xml ×1