我有 Webservice 的奇怪行为。
现在网络服务看起来像:
[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1
{
// To use HTTP GET, add [WebGet] attribute. (Default ResponseFormat is WebMessageFormat.Json)
// To create an operation that returns XML,
// add [WebGet(ResponseFormat=WebMessageFormat.Xml)],
// and include the following line in the operation body:
// WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";
[OperationContract]
public string DoWork()
{
// Add your operation implementation here
return "Hello there";
}
// Add more operations here and mark them with [OperationContract]
}
我可以毫无问题地在 JS 中读取它:
<script>
function doWork() {
Service1.DoWork(onSuccess, onFailure);
}
function onSuccess(result) {
document.getElementById('txtValueContainer').value = result;
}
function onFailure(result) {
window.alert(result);
}
</script>
但是,一旦我尝试发送其他内容,例如字符串:
[OperationContract]
public Question[] OurServerOutput(string userid)
{
return Question.getQuestionsForUser(new Guid(userid)).ToArray();
}
where
public static IEnumerable<Question> getQuestionsForUser( Guid userGuid)
{
LinqConnectionDataContext context = new LinqConnectionDataContext();
var query = from c in context.Questions where c.UidUser == userGuid select c;
return query;
}
Javascript 无法再找到我的服务,并且出现错误:
Unhandled exception at line 132, column 13 in http://localhost/
0x800a1391 - Microsoft JScript runtime error: 'Service1' is undefined
如何通过启用 Ajax 的 WebService 发送数组?