I’ve created a simple ASP.NET web application, with a basic webservice. In the client side I’ve got Javascript code that retrieves data from the webservice using AJAX.
My webservice generates an student object with some basic data (name, street, phone) and sends it to the client via a JS call.
The client has a button that fires the call to the webservice via JS.
My issue is that when I click the JS button to get data from the webservice I always get this error message related to my JS webservice call function:
“JavaScript runtime error: Unable to get property 'GetStudentId' of undefined or null reference”
How can I fix this?
my home.aspx page:
<%@ 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 (don't mind the json prefix name of the file)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace Ajax_testing
{
/// <summary>
/// Summary description for jsonwebservice
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class jsonwebservice : System.Web.Services.WebService
{
[WebMethod]
public Student GetStudentId()
{
Student student = new Student();
student.name = "S.L. Holmes";
student.street = "baker street";
student.phone = "27673627";
return student;
}
}
public class Student
{
public string name { get; set; }
public string street { get; set; }
public string phone { get; set; }
}
}
my java script: 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());
}