首页 新闻 赞助 找找看

jquery 调用webservice

0
悬赏园豆:20 [已解决问题] 解决于 2015-05-19 10:24

现在有一个webservice,但是我需要在其他项目中调用它,而且只能在jquery内写,尝试了很多方式,结果都不理想,希望大神们帮帮我

webservice

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace WebApplication1
{
    /// <summary>
    /// WebService1 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。 
     [System.Web.Script.Services.ScriptService]
    public class WebService1 : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }
    }
}

html页面

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <script src="Scripts/jquery-1.8.2.js"></script>
     <script type="text/javascript">
         $(document).ready(function () {
             $("#btnClick").click(function () {
                 $.ajax({
                     url: "http://localhost:43563/WebService1.asmx/HelloWorld",
                     beforeSend: function (x) {
                         x.setRequestHeader("Content-Type", "application/json; charset=utf-8");
                     },
                     data: {},
                     dataType: "json",
                     type: "POST",
                     error: function (x, e) {
                         alert(x.responseText);
                     },
                     complete: function (x) {
                         //alert(x.responseText); 
                     },
                     success: function (data) {
                         var msg = data.d;
                         var json = JSON2.parse(msg);
                         alert(json.id);
                     }
                 });

             });
         });
    </script>
    
</head>
<body>
    <input type="button" name="btnClick" id="btnClick" value="CLICK ME"  />
</body>
</html>

html是另一个项目中的,和webservice并不在同一项目中

源坊的主页 源坊 | 初学一级 | 园豆:9
提问于:2015-05-13 20:33
< >
分享
最佳答案
0

 提供一个思路, 如果这样不行的话, 你可以在后台调用webservice, 然后ajax访问你的后台得到webservice的东东

收获园豆:10
请叫我头头哥 | 大侠五级 |园豆:9382 | 2015-05-14 10:23

其实html那个项目没有后台,只是单纯的html,不过问题已经解决了,事后会写篇博客来记录下

源坊 | 园豆:9 (初学一级) | 2015-05-14 11:58

@源坊: 这绝对是个好习惯, 解决了就结贴呗  :)

请叫我头头哥 | 园豆:9382 (大侠五级) | 2015-05-14 18:30
其他回答(3)
0

结果哪里不理想?

dudu | 园豆:31075 (高人七级) | 2015-05-13 21:05

就是进入了error

 

支持(0) 反对(0) 源坊 | 园豆:9 (初学一级) | 2015-05-14 08:37
0

webservice还包装了一层soap协议,你随便baidu,google也可以搜出很多例子来,压根不用这里问

收获园豆:5
arg | 园豆:1047 (小虾三级) | 2015-05-14 00:33

能给个链接吗

支持(0) 反对(0) 源坊 | 园豆:9 (初学一级) | 2015-05-14 08:37
0
收获园豆:5
雨之秋水 | 园豆:649 (小虾三级) | 2015-05-14 09:23
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册