如何使用jQuery来调用Web服务在HTML页面? [英] How to call webservice in HTML page using JQuery?

查看:150
本文介绍了如何使用jQuery来调用Web服务在HTML页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有web服务得到2串并返回,如果他们等于

i have WebService that get 2 string and return if they equals

的WebService,在 HTTP://192.168.100.MyIP/HTML_SIMPLE_TEST/

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

[System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services.WebService
{
     [WebMethod(Description = "Validate the login credentials")]
     public bool Validate(string UserName, string Password)
     {
          if (Password == "test" && UserName == "test")
              return true;
          else
              return false;
     }
}

和我有html页面调用此WebService,需要返回结果

and i have html page that call this WebService and need to return the Result

<html  lang="en-US">
    <head>
        <meta http-equiv="content-type" content="application/xhtml+xml; charset=UTF-8" />
        <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
        <script type="text/javascript" language="javascript">

        var Surl = "http://localhost:3031/WS_HTML/Service1.asmx/Validate";

        $(document).ready(function () {
            $("input#submit").click(function (event) {
                //var uid = document.getElementById("UserName").value;
                //var pwd = document.getElementById("Password").value;

                var uid = "test";
                var pwd = "test";

                var dataString = "{ 'UserName' : '" + uid + "', 'Password' : '" + pwd + "'}";

                $.ajax({
                    ServiceCallID: 1,
                    url: Surl,
                    type: 'POST',
                    data: dataString,
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",

                    success: function (result) {
                        returnVal = result.d;
                        alert(returnVal);
                    },

                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        returnVal = '';
                    }
                });
            });
        });

</script>

</head>
    <body >
        <form method=post runat="server">
            <input  type="submit" />Connet to my WS
        </form>
    </body>
</html>

这就是我得到:

this what i get:

和我试试这个,还是同样的问题......

and i try this, still same problem....

<script>
    function XX() {
        alert("in");
        $.post('http://localhost/WS_HTML/Service1.asmx/Validate', { username: 'test', password: 'test' }, function (response) {
            response; // Here is response
            alert(response);
        });
    }
</script>

</head>
    <body >
         <form method=post runat="server">
            <input  type="submit" onclick="XX();" />Connet to my WS2
         </form>
    </body>
</html>

我尝试用 $获得 - 和一样的....什么也没有发生......

i try with $.get - and same.... Nothing happens....

从浏览器的工作。如果我这样写:本地主机/ WS_HTML / Service1.asmx的我看到我的WS,

from the browser its work. if i write this: localhost/WS_HTML/Service1.asmx i see my WS,

但如果我这样写:本地主机/ WS_HTML / Service1.asmx的/验证我看到浏览器错误=>

but if i write this: localhost/WS_HTML/Service1.asmx/Validate i see error on browser =>

请求格式是无法识别的网址/验证意外结束。

我向上帝发誓,这几天我打破我的头,想不通为什么它不工作

I swear to God that for several days I break my head and can not figure out why it does not work

):

感谢

推荐答案

为什么不使用$。员额()?

Why not use $.post() ?

$.post('/your/service/url/', {username:'asd', password:'qwe'}, function(response){
    response; // Here is response
});

这篇关于如何使用jQuery来调用Web服务在HTML页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆