未捕获的SyntaxError:意外的令牌<从ajax调用webservice时

时间:2014-03-26 06:51:15

标签: jquery ajax json web-services

 $(document).ready(function () {
    $("#submit").click(function (e) {
        e.preventDefault();
        var userName = $("#username").val();
        var password = $("#password").val();
        authenticate(userName, password);
    });
 });

 function authenticate(userName, password) {
    var serviceurl = "amismobilesapps/amismobileservices.asmx?op=getUserAuthentication";
    var postdata = JSON.stringify({
        "userName": JSON.stringify($("#username").val()),
        "password": JSON.stringify($("#password").val())
    });
    $.ajax({
        url: serviceurl,
        type: 'POST',
        contentType: "text/javascript;charset=utf-8",
        data: postdata,
        dataType: 'JSONP',
        async: true,
        crossDomain: true,
        jsonp: 'callback',
        jsonpCallback: 'jsonpCallback',
        success: function (msg) {
            alert('Web service call succeeded.  ' + msg.d);
        },
        error: function (error) {
            alert('ERROR has occurred!');
            alert(JSON.stringify(error))
        }
    })
 }

我的网站是:

[WebService(Namespace = "http://apps.avineonindia.com/")][WebServiceBinding(ConformsTo =WsiProfiles.BasicProfile1_1)]// [ToolboxItem(false)[System.ComponentModel.ToolboxItem(false)][System.Web.Script.Services.ScriptService]
public class amismobileservices : System.Web.Services.WebService
{ 
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["AMISTEST"].ToString());

    [WebMethod]
    [System.Security.Permissions.PrincipalPermission(System.Security.Permissions.SecurityAction.Assert, Unrestricted = true)] 
    [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
    public bool getUserAuthentication(string UserName, string pwd)
    { 
        con.Open();
        SqlCommand cmd = new SqlCommand("select  * from EMPLOYEE_MAS where Login_name='" + UserName + "' and password='" + pwd + "'", con);
        int n = Convert.ToInt32(cmd.ExecuteScalar());
        if (n >= 1)
            return true;
        else 
            return false;

    } 
}

2 个答案:

答案 0 :(得分:0)

使用

var postdata = JSON.stringify({
    "userName": $("#username").val(), //You don't need to use JSON.stringify here
    "password": $("#password").val()
});

而不是

var postdata = JSON.stringify({
    "userName": JSON.stringify($("#username").val()),
    "password": JSON.stringify($("#password").val())
});

此外,当您传递userName, password作为参数时,请直接使用它们。像

var postdata = JSON.stringify({
    "userName": userName, 
    "password": password
});

答案 1 :(得分:0)

如果你想通过ajax调用webservice,你需要在你的webservice名称之后写出直接的方法名称... e.g :webservice.asmx/methodname

     function authenticate(userName, password) {
//change  url here 
        var serviceurl = "amismobilesapps/amismobileservices.asmx/getUserAuthentication";
        var postdata = JSON.stringify({
            "userName": JSON.stringify($("#username").val()),
            "password": JSON.stringify($("#password").val())
        });
        $.ajax({
            url: serviceurl,
            type: 'POST',
            contentType: "text/javascript;charset=utf-8",
            data: postdata,
            dataType: 'JSONP',
            async: true,
            crossDomain: true,
            jsonp: 'callback',
            jsonpCallback: 'jsonpCallback',
            success: function (msg) {
                alert('Web service call succeeded.  ' + msg.d);
            },
            error: function (error) {
                alert('ERROR has occurred!');
                alert(JSON.stringify(error))
            }
        })
     }