Question

Good afternoon to everyone, I want to make a post and I already read some forums and I get this but it doesn't work.

This is my html file

<!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>
    <title></title>
    <script src="http://code.jquery.com/jquery-1.10.2.min.js" type="text/javascript"></script>
</head>
<script type="text/javascript">
    function run_javascript_function() {
        $.ajax({
            type: "POST",
            url: "testclass.aspx/getdata",
            //contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
                alert(data.d);
            },

            error: function () { alert("Ajax Error"); }
        });
        alert("Data from JavaScript");
    }
</script>
<body>
    <input type="button" value="Run Jquery or Javascript Function" onclick="run_javascript_function()" />
</body>
</html>

and this is testclass.aspx file

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="testclass.aspx.vb" Inherits="testclass" %>

<!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">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

    </div>
    </form>
</body>
</html>

and this is the code file

Imports System.Web.Services
Imports System.Web.Script.Services

Partial Class testclass
    Inherits System.Web.UI.Page

    <WebMethod()> _
    <ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
    Public Shared Function getdata() As String
        Return "some data from post"
    End Function

End Class

When I run this script I get "Ajax Error" instead "Some data from post", do you have any idea how to make this, and if I missing something on webconfig or something else?

Was it helpful?

Solution 2

I already fix it, so I added the script in <head> tag but I had to enable the line contentType: "application/json; charset=utf-8", on my script and it works fine!, I worked last year with PHP and I didn't need to add my scripts on head or body tags, they work anyway but here is a little bit different but it's ok, thanks @afzalulh

OTHER TIPS

Your run_javascript_function() function with it's <script> tag is sitting nowhere between <head> and <body>. Just move it inside <head> tag:

<head>
    <title></title>
    <script src="http://code.jquery.com/jquery-1.10.2.min.js" type="text/javascript"></script>

    <script type="text/javascript">
        function run_javascript_function() {
            $.ajax({
                type: "POST",
                url: "testclass.aspx/getdata",
                //contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) {
                    alert(data.d);
                },

                error: function () { alert("Ajax Error"); }
            });
            alert("Data from JavaScript");
        }
    </script>
</head>

Here's the result:

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top