【问题标题】:Problems with jquery and C# web servicejquery 和 C# web 服务的问题
【发布时间】:2012-07-18 10:53:58
【问题描述】:

来自网络服务的输出,“http://localhost:6833/Service1.asmx/HelloWorld”:

<string xmlns="http://tempuri.org/">
[{"Name":"Pini","Age":"30","ID":"111"},{"Name":"Yaniv","Age":"31","ID":"Cohen"},{"Name":"Yoni","Age":"20","ID":"Biton"}]
</string>

HTML 代码:

<html>
<head>
<script type="text/javascript" src="jquery-1.7.2.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$.ajax({
        type: "POST", 
        url: "Service1.asmx/HelloWorld", 
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            alert("Result: " + msg);
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            alert("Error: " + textStatus);
    }
});
});
</script>
</head>
<body>
</body>
</html>

当我在浏览器中运行 index.html 时,我收到错误警报。尝试了很多东西,但无法找出丢失的内容。

【问题讨论】:

  • 只有错误:错误。但不确定该功能是如何工作的,这都是新的
  • 试试这个警报(“错误:” + errorThrown);告诉我你得到了什么。
  • 认为是的,尝试过:“localhost:6833/Service1.asmx/HelloWorld”和“Service1.asmx/HelloWorld”。第一个在我的浏览器中运行良好。
  • 试试“~/Service1.asmx/HelloWorld”
  • 由于路径有问题,请尝试“../../Service1.asmx/HelloWorld”或“/Service1.asmx/HelloWorld”或使用完整路径,您可以稍后将其更改为你的域名。

标签: c# javascript jquery web-services


【解决方案1】:

你的 json 封装在一个 xml 字符串中,我想这就是你的问题。

编辑:

查看this 帖子了解更多信息。

【讨论】:

    【解决方案2】:

    尝试在你的方法之前添加它。

     [ScriptMethod(ResponseFormat = ResponseFormat.Json)] 
    

    下面

     [WebMethod]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-03
      相关资源
      最近更新 更多