首页 新闻 赞助 找找看

$.ajax的问题

0
悬赏园豆:10 [已解决问题] 解决于 2012-07-12 00:27

脚本代码:

 1 $.ajax({
 2                 url: "http://localhost:5311/UserService.asmx/GetLoginId",
 3                 type: "POST",
 4                 contentType: "application/json",
 5                 data: '{"loginId": "a"}',
 6                 dataType: "json",
 7                 beforeSend:function (){
 8                     alert("准备调用");
 9                 },
10                 success: function (e) {
11                     window.alert("调用成功");
12                 },
13                 error:function  (){
14                     alert("发生错误");
15                 }
16             });

 WebService代码:

1     [WebMethod]
2     public bool GetLoginId(string loginId)
3     {
4         return "abc";
5     }

我实验了下,直接通过浏览器访问http://localhost:5311/UserService.asmx 的GetLoginId方法可以正常获取期望值.通过一个简单html页面form表单post提交方式也可以正常获取期望值.就是无法通过jquery的$.ajax方法获取期望值甚至报错!!!我也尝试过自己写一个ajax方法依旧无法获取期望值,我为了测试是否有访问到该链接,写了个方法测试结果证明$.ajax函数和自己写的ajax函数都没有请求该链接,也难怪无法获取期望值.

通过浏览器访问返回的结果是

 

<?xml version="1.0" encoding="utf-8"?>

 

<boolean xmlns="http://tempuri.org/">true</boolean>

我尝试把该链接的内容运行环境换了两种方式,一种是Visual Studio开发环境,一种是IIS运行环境,也都失败告终.

我想要说的就是无论哪种运行环境,哪种javascript脚本,哪种方式都好,只要能获取到期望值就好了,

不知道各位有什么看法,见解,答案之类的,愿聆听之~~

VAllen的主页 VAllen | 初学一级 | 园豆:94
提问于:2012-07-11 13:55
< >
分享
最佳答案
0
返回的json 默认给了d属性,用下面的方式获取, 我在项目中就是这样用的

$.ajax({
        type: "POST",   //访问WebService使用Post方式请求
        contentType: "application/json", //WebService 会返回Json类型
        async: false,
        //url: "../WebService.asmx/BrandDate", //调用WebService的地址和方法名称组合 ---- WsURL/方法名
        
        url: "../WebService.asmx/BrandDateStr",
        data: "{'ProductID':"+productid+",'BrandID':"+brandid+"}",         //这里是要传递的参数,格式为 data: "{paraName:paraValue}",下面将会看到       
        dataType:"json",
        success: function(result) {     //回调函数,result,返回值
             datastr= result.d;             
        }
    });
收获园豆:10
BiakeChou | 菜鸟二级 |园豆:204 | 2012-07-11 15:50

那么,很好,麻烦你试一下把url路径改为网络路径,例如http://localhost:80/WebService.asmx/BrandDate这样的网络路径,而不是本地路径,我也曾用过本地路径做测试,没有问题,唯独用网络路径测试有问题.你可以尝试把你的asmx单独做成一个WebService网站项目,然后用IIS在本机发布它,然后再另外一个aspx页面测试一下试试..

VAllen | 园豆:94 (初学一级) | 2012-07-11 17:36

@VAllen: 你的意思是比如本地测试路径是 http://localhost:64,   webservice也在这个站点中把url改成 http://localhost:64/webService.asmx/BrandDate 也是可以的 我刚测试过,如果新建一个站点 http://localhost:65 用于返回 ajax数据到第一个本地测试的站点上?

那肯定是不行的 因为这是跨域访问了,要用jsonp方式跨域获取,jquery也封装了jsonp的ajax方法, 也是用$.ajax, dataType为"jsonp", jsonp:"jsonpcallback" 和普通$.ajax的使用有点区别,返回值 jsonpcallback+ "({" + response + "})", 

BiakeChou | 园豆:204 (菜鸟二级) | 2012-07-11 21:17

@BiakeChou: ~~花了很长时间才搞定,早点看到你的回复我就不用再浪费那么多时间了,呵呵...谢谢你啦!!!

正确的解决方案:

web.config配置文件:

1 <system.web>
2     <webServices>
3       <protocols>
4         <add name="HttpPost"/>
5         <add name="HttpGet"/>
6       </protocols>
7     </webServices>
8 </system.web>

WebService代码:

 

 1 using System.Web;
 2 using System.Web.Services;
 3 /// <summary>
 4 ///UserService 的摘要说明
 5 /// </summary>
 6 [WebService(Namespace = "http://tempuri.org/")]
 7 [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
 8 public class UserService : System.Web.Services.WebService
 9 { 
10     [WebMethod]
11     public void GetLoginId(string loginId)
12     {
13         string callback = HttpContext.Current.Request["jsoncallback"];
14         bool bl= true;//这是我调用业务逻辑层(BLL)的一个方法
15                               //返回一个布尔(boolean)值
16                               //现在我省略掉,直接赋值true
17         HttpContext.Current.Response.Write(callback +
18             "({result:'" + bl + "'})");
19         //关于result这词是你自己自定义的属性
20         //会作为回调参数的属性供你调用结果
21         HttpContext.Current.Response.End();
22     }
23 }

 

aspx页面及javascript脚本代码:

 

 1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
 2  
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 4 <html xmlns="http://www.w3.org/1999/xhtml">
 5 <head runat="server">
 6     <title>测试</title>
 7     <script type="text/javascript" src="Scripts/jquery-1.7.2.js"></script>
 8     <script type="text/javascript">
 9         //Document加载完毕后初始化方法
10         $(function Init() {
11             $("#TxtLoginId").bind("blur", CkLoginId);
12         }); 
13         //帐号验证及提示
14         function CkLoginId() {
15             var Id = $("#TxtLoginId");
16             $.ajax({
17                 url: "http://localhost:5311/UserService.asmx/GetLoginId?jsoncallback=?",
18                 dataType:"jsonp",
19                 data:{"loginId":Id.val()},
20                 success:OnSuccess,
21                 error:OnError
22             });
23         }
24         function OnSuccess(json) {
25             alert(json.result);
26         }
27         function OnError(XMLHttpRequest, textStatus, errorThrown) {
28             targetDiv = $("#data");
29             if (errorThrown || textStatus == "error" || textStatus == "parsererror" || textStatus == "notmodified") {
30                 targetDiv.replaceWith("请求数据时发生错误!");
31                 return;
32             }
33             if (textStatus == "timeout") {
34                 targetDiv.replaceWith("请求数据超时!");
35                 return;
36             }
37         }  
38     </script>
39 </head>
40 <body>
41     <form id="form1" runat="server">
42     <table border="0" cellspacing="0" cellpadding="0" width="100%">
43         <tr>
44             <td>
45                 <asp:Label ID="LblLoginId" runat="server" Text="帐&nbsp;&nbsp;号" ClientIDMode="Static"></asp:Label>
46                 <asp:TextBox ID="TxtLoginId" runat="server" ClientIDMode="Static"></asp:TextBox>
47             </td>
48         </tr>
49     </table>
50     </form>
51 </body>
52 </html>

 

运行结果:Ture

 

VAllen | 园豆:94 (初学一级) | 2012-07-12 00:25
其他回答(3)
0

语言是各有各的优点。要扬长避短的运用语言。

悟行 | 园豆:12559 (专家六级) | 2012-07-11 13:58
0

是不是你写的有问题啊,我在一个论坛中看见这篇文章,按照上面的写了下,可以。

比如你的web服务有没有去掉[System.Web.Script.Services.ScriptService] 的注释

"loginId"这里是否需要加双引号呢,
"a"是否是单引号
有时候一些细节上的东西往往不容易被人们发现,但这些却又是致命的

http://www.dobug.net/showtopic-83.html

pasig10038 | 园豆:387 (菜鸟二级) | 2012-07-11 14:18

我查了那么多资料,怎么可能没有注意到这个问题呢,肯定有去掉注释的啦..

依旧不行..至于data这部分内容的写法我也换了很多种,也不行

支持(0) 反对(0) VAllen | 园豆:94 (初学一级) | 2012-07-11 17:30
0

   [System.Web.Script.Services.ScriptService]

 

贴主肯定是把这句话给注释掉了!!

需要格局 | 园豆:2145 (老鸟四级) | 2012-07-11 14:38

刚创建好,默认就是注释的,但是我在编写代码的时候就已经把它去掉注释了

支持(0) 反对(0) VAllen | 园豆:94 (初学一级) | 2012-07-11 17:31
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册