PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, September 8, 2022

[FIXED] Why i cannot use $.ajax

 September 08, 2022     ajax, asp.net, javascript     No comments   

Issue

I have one problem on my project. I would like access my function when iframe is over, but i try search how i can do this, but all solutions not worked, i dont know why? My project have templates of Asp.Net Web Application (.NET Framework).

Script js, when create a iframe and "try" connect to my function on WebForm1.aspx.cs:

<script>
    const tag = document.createElement("script");
    tag.id = "iframe-demo";
    tag.src = "https://www.youtube.com/iframe_api";
    const [firstScriptTag] = document.getElementsByTagName("script");
    firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
    let player;

    window.onYouTubeIframeAPIReady = () => {
        player = new window.YT.Player("player", {
            width: 500,
            height: 500,
            videoId: "M7lc1UVf-VE",
            events: {
                onStateChange: window.onPlayerStateChange
            }
        });
    };

    window.onPlayerStateChange = (event) => {
        if (event.data === 0) {
            jQuery.ajax({/* * */
                method: "POST",
                url: "/WebForm1.aspx/returnid",
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    console.log(textStatus);
                },
                success: function (result) {
                    console.log("success");
                }
            });
        }
    };
</script>

Code of WebForm1.aspx.cs:

[WebMethod]//[WebMethod()]
    public static string returnid(string subject, string message, string messageId, string pupilId)
    {
        Console.WriteLine("testar");

        return "";//https://www.youtube.com/watch?v=8elyZ6aGgZY
    }

I try use $.ajax, ajax, $Jquery there * and dont work. 1


Solution

Try calling the AJAX this way, to see if will hit the method:

jQuery.ajax({
        method: "POST",
        url: "WebForm1.aspx/returnid",
        contentType: "application/json; charset=utf-8",        
        dataType: "json",
        data: JSON.stringify({ subject: "subject", message: "message", messageId: 1, pupilId: 1 }),
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            console.log("Error", errorThrown);
        },
        success: function (result) {
            console.log("success", result);
        }
    });


Answered By - Paulo Fernando
Answer Checked By - Mildred Charles (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing