Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

enter image description here

"ERROR JS message" "Mysolution explorer"

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());
}
share|improve this question

1 Answer 1

You define your server-side object as:

Ajax_testing.jsonwebservice

Then call your client-side object as:

Ajax_testing.jasonwebservice

There's an extra a in the client-side version.

I'm honestly surprised if this client-side invocation works at all. Does that server-side code really emit client-side proxy objects automatically? I've never seen that happen, but that's pretty awesome if it does.

share|improve this answer
    
Omg shame on me! It works, data gets passed to the div (id=myTestDiv), but after a fraction of a second the page gets reloded as, with a new post back and the is still empty. –  Luther Jun 26 at 18:58
1  
@David that makes 2 of us. Never realized this existed smh msdn.microsoft.com/en-us/library/… –  Rodney Golpe Jun 26 at 19:03
    
My post back was due to the lack of type="button" attribute <button onclick="GetStudentById()" type="button">, careless mistakes. I'll fix it the question. –  Luther Jun 26 at 19:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.