Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过AJAX从Web服务(asp.net)检索数据时发生Javascript错误_Javascript_Asp.net_Ajax_Web Services_Soap - Fatal编程技术网

通过AJAX从Web服务(asp.net)检索数据时发生Javascript错误

通过AJAX从Web服务(asp.net)检索数据时发生Javascript错误,javascript,asp.net,ajax,web-services,soap,Javascript,Asp.net,Ajax,Web Services,Soap,我创建了一个简单的ASP.NET web应用程序,其中包含一个基本的web服务。在客户端,我得到了使用AJAX从Web服务检索数据的Javascript代码 MyWebService生成一个包含一些基本数据(姓名、街道、电话)的学生对象,并通过JS调用将其发送给客户端 客户端有一个按钮,通过JS触发对webservice的调用 我的问题是,当我单击JS按钮从webservice获取数据时,总是会收到与我的JS webservice调用函数相关的错误消息: “JavaScript运行时错误:无法获

我创建了一个简单的ASP.NET web应用程序,其中包含一个基本的web服务。在客户端,我得到了使用AJAX从Web服务检索数据的Javascript代码

MyWebService生成一个包含一些基本数据(姓名、街道、电话)的学生对象,并通过JS调用将其发送给客户端

客户端有一个按钮,通过JS触发对webservice的调用

我的问题是,当我单击JS按钮从webservice获取数据时,总是会收到与我的JS webservice调用函数相关的错误消息:

“JavaScript运行时错误:无法获取未定义或空引用的属性'GetStudentId'”

我怎样才能解决这个问题

my home.aspx页面:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="home.aspx.cs" Inherits="Ajax_testing.home" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <%--<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js" type="text/javascript"></script>--%>
    <script type="text/javascript" src="script/myjs.js"></script>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
        <Services>
            <asp:ServiceReference Path="~/jsonwebservice.asmx" />
        </Services>
    </asp:ScriptManager>
    <button onclick="GetStudentById()" type="button">GET AJAX DATA FROM WEBSERVICE</button>
    <a href="jsonwebservice.asmx">to JSON webservice</a>
    <div id="myTestDiv">
    </div>
    <div id="jsontest">
    </div>
    </form>
</body>
</html>

my webservice jsonwebservice.asmx(不介意文件的json前缀名)

使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.Services;
名称空间Ajax\u测试
{
/// 
///jsonwebservice的摘要说明
/// 
[WebService(命名空间=”http://tempuri.org/")]
[WebServiceBinding(ConformsTo=WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
//要允许使用ASP.NET AJAX从脚本调用此Web服务,请取消注释以下行。
[System.Web.Script.Services.ScriptService]
公共类jsonwebservice:System.Web.Services.WebService
{
[网络方法]
公立学生GetStudentId()
{
学生=新生();
student.name=“S.L.Holmes”;
student.street=“贝克街”;
student.phone=“27673627”;
留学生;
}
}
公立班学生
{
公共字符串名称{get;set;}
公共字符串street{get;set;}
公用字符串电话{get;set;}
}
}
我的java脚本:myjs.js

function GetStudentById() {
    Ajax_testing.jasonwebservice.GetStudentId(GetStudentByIdSuccessCallBack, 
GetStudentByIdFailedCallback); 
//*******correction: jasonwebservice -> jsonwebservice****
}


function GetStudentByIdSuccessCallBack(results) {
    document.getElementById("myTestDiv").innerHTML = "JSON webservice <br/> " + results.name + "<br/>" + results.street + "<br/>" + results.phone + "<br/>";
}


function GetStudentByIdFailedCallback(errors) {
    alert(errors.get_message());
}
函数GetStudentById(){ Ajax_testing.jasonwebservice.GetStudentId(GetStudentByIdSuccessCallBack, GetStudentByIdFailedCallback); //*******更正:jasonwebservice->jsonwebservice**** } 函数GetStudentByIdSuccessCallBack(结果){ document.getElementById(“myTestDiv”).innerHTML=“JSON webservice
”+results.name+”
“+results.street+”
“+results.phone+”
”; } 函数GetStudentByIdFailedCallback(错误){ 警报(错误。获取消息()); }
您将服务器端对象定义为:

Ajax_testing.jsonwebservice
Ajax_testing.jasonwebservice
然后将客户端对象称为:

Ajax_testing.jsonwebservice
Ajax_testing.jasonwebservice
客户端版本中有一个额外的
a


我真的很惊讶这种客户端调用是否能工作。服务器端代码真的会自动发出客户端代理对象吗?我从未见过这种情况发生,但如果真的发生了,那就太棒了。

天哪,我真是太遗憾了!它起作用了,数据被传递到div(id=myTestDiv),但几秒钟后,页面被重新编码为,并返回了一篇新文章,并且仍然是空的。@David,这让我们两个都感到惊讶。我从来没有意识到这是因为缺少type=“button”属性和粗心的错误。我会解决这个问题的。