调用简单的Web服务(.asmx文件)使用JSON AJAX和JQuery的 - 解析错误错误、简单、文件、Web

2023-09-04 00:48:39 作者:那就让我一个人来承担这份痛吧

结交尝试使用所有这些技术融合在一起了第一步..我有一些toubles .. 这是我的服务器端:

Making my first steps in trying to use all these technologies together.. I'm having some toubles.. Here is my Server side:


[WebMethod(EnableSession = true)]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string simplestMethod()
{
  return "Simplest method returned";
}

这里是我的客户端:

And here is my client side:

 $(document).ready(function(){
   $("a").click(function(event){     
      $.ajax({
      type: "POST",
      url: "http://localhost:53346/d2/TAPI.asmx/simplestMethod",
      data: "{}",
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      success: function (data) {
       alert(data.d);
      },
      error: function (XMLHttpRequest, textStatus, errorThrown) {
       alert("Error Occured!" +" | " + XMLHttpRequest +" | " + textStatus +" | " + 
       errorThrown );
      }
   });
  });
 });

结果是一个警告,说: 发生错误! | [对象XMLHtt prequest] | parseerror |未定义。 失败了,为什么什么分析? 我应该指出,直接调用WS方法确实可行。 非常感谢!

The result is an alert that says: Error Occured! | [object XMLHttpRequest] | parseerror | undefined. What parsing failed and why? I should mention that calling the WS method directly does work. Thanks a lot!

推荐答案

您code看起来好有一个可疑的地方:网​​址。您应该替换网​​址来像TAPI.asmx / simplestMethod/ D2 /TAPI.asmx/simplestMethod

Your code looks like good with one suspected place: url. You should replace url to something like "TAPI.asmx/simplestMethod" or "/d2/TAPI.asmx/simplestMethod".

此外,如果你想学习到如何调用Web方法与参数或从Web方法看看返回更复杂的数据http://stackoverflow.com/questions/2737525/how-do-i-build-a-json-object-to-send-to-an-ajax-webservice/2738086#2738086和http://stackoverflow.com/questions/3445859/asmx-web-service-json-javascript-jquery/3446517#3446517, http://stackoverflow.com/questions/2670147/can-i-return-json-from-an-asmx-web-service-if-the-contenttype-is-not-json/2671583#2671583.如何去code错误消息从异常Web方法里面看到http://stackoverflow.com/questions/3635133/get-xhr-object-in-vb-net-while-ajax-calling-fails/3644248#3644248.

Moreover if you want study to how to call web method with parameters or return more complex data from the web method look at http://stackoverflow.com/questions/2737525/how-do-i-build-a-json-object-to-send-to-an-ajax-webservice/2738086#2738086 and http://stackoverflow.com/questions/3445859/asmx-web-service-json-javascript-jquery/3446517#3446517, http://stackoverflow.com/questions/2670147/can-i-return-json-from-an-asmx-web-service-if-the-contenttype-is-not-json/2671583#2671583. How to decode error messages from the exception inside of web method see http://stackoverflow.com/questions/3635133/get-xhr-object-in-vb-net-while-ajax-calling-fails/3644248#3644248.