locked
JQ ajax on web form error RRS feed

  • Question

  • User1922541779 posted

    Hi   I copied example of ajax and tried to run it. I am using .net 4.6.1  web form. I am getting error: Unchecked runtime.lastError: Could not establish connection. Receiving end does not exist.

    is this CORS issue? Please help

    <%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Ajax._Default" %>
    
    <asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
    
        <div id="div1"><h2>Let jQuery AJAX Change This Text</h2></div>
    
    <button>Get External Content</button>
        <script>
            $(document).ready(
                function () {
                    $('button').click(
    
                    function () {
                        $.ajax({
                            url: "./test.txt",
                            
                            success: function (data) {
    
                                $('#div1').innerHtml = data;
                            }
                        });
                    })
                    }
    );
    
        </script>
    
    </asp:Content>

    I am getting run time error:Unchecked runtime.lastError: Could not establish connection. Receiving end does not exist.

    Wednesday, March 6, 2019 10:19 PM

Answers

  • User475983607 posted

    the error message is gone but the the text on the page is not replaced by the text in test.txt as expected 

    Your original jQuery syntax also has a bug.  Working example where the text.txt file is one folder level above the current folder.

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AjaxDemo.aspx.cs" Inherits="WebFormsApp.AjaxDemos.AjaxDemo" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>AJAX Demo</title>
        <script src="/Scripts/jquery-3.3.1.js"></script>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <div id="div1">
                    <h2>Let jQuery AJAX Change This Text</h2>
                </div>
                <button>Get External Content</button>
            </div>
        </form>
        <script>
            $(document).ready(
                function () {
                    $('button').click(function (e) {
                        e.preventDefault();
                        $.ajax({
                            url: "../test.txt",
                            success: function (data) {
                                console.log(data);
                                $('#div1').html(data);
                            }
                        });
                    })
                });
        </script>
    </body>
    </html>
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, March 9, 2019 12:02 AM

All replies

  • User475983607 posted

    The URL is invalid.

    url: "../test.txt",

    Wednesday, March 6, 2019 10:58 PM
  • User1922541779 posted

    i tried   "../test.txt" it did not work

    Wednesday, March 6, 2019 11:43 PM
  • User-1038772411 posted

    The file "test.txt" is in the same directory or somewhere else? The path needs to reach the file 

    also , add following to the code

    //This should do the trick

    $(document).ready(function() {
    $("button").click(function(){
    $.ajax({
    Type: "GET"
    url: "test.txt",
    dataType: "text",
    success: function(result) {
    //for debug
    alert(result);
    $("#div1").html(result);
    },
    error: function(e) {
    alert(e)
    }
    });
    //Here's another option
    $("#div1").load("test.txt");
    });
    });



    Thursday, March 7, 2019 5:42 AM
  • User1120430333 posted

    What is a URL?

    https://www.verisign.com/en_US/website-presence/online/what-is-a-url/index.xhtml

    What is AJAX?

    https://www.seguetech.com/ajax-technology/

    What is a HTTP Get?

    https://www.w3schools.com/tags/ref_httpmethods.asp

    What is a Restul API

    https://searchmicroservices.techtarget.com/definition/RESTful-API

    How is it that tezt.text being used as a URL meet the above usage?

    Thursday, March 7, 2019 6:20 AM
  • User839733648 posted

    Hi kobosh,

    kobosh

    I am getting run time error:Unchecked runtime.lastError: Could not establish connection. Receiving end does not exist.

    The error is often caused by a chrome extension.

    I suggest that you could try disabling all your extensions, the problem may disapear.

    Best Regards,

    Jenifer

    Thursday, March 7, 2019 6:54 AM
  • User1922541779 posted

    thanks it did not work

    Friday, March 8, 2019 11:03 PM
  • User1922541779 posted

    thanks  Jenifer I disabled all chrome extensions.....the error message is gone but the the text on the page is not replaced by the text in test.txt as expected 

    Friday, March 8, 2019 11:07 PM
  • User475983607 posted

    the error message is gone but the the text on the page is not replaced by the text in test.txt as expected 

    Your original jQuery syntax also has a bug.  Working example where the text.txt file is one folder level above the current folder.

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AjaxDemo.aspx.cs" Inherits="WebFormsApp.AjaxDemos.AjaxDemo" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>AJAX Demo</title>
        <script src="/Scripts/jquery-3.3.1.js"></script>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <div id="div1">
                    <h2>Let jQuery AJAX Change This Text</h2>
                </div>
                <button>Get External Content</button>
            </div>
        </form>
        <script>
            $(document).ready(
                function () {
                    $('button').click(function (e) {
                        e.preventDefault();
                        $.ajax({
                            url: "../test.txt",
                            success: function (data) {
                                console.log(data);
                                $('#div1').html(data);
                            }
                        });
                    })
                });
        </script>
    </body>
    </html>
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, March 9, 2019 12:02 AM
  • User1922541779 posted

    thanks megebhard........ url:"test.txt"    worked fine ; I can see data in alert(data) correctly.....but still the  data is not bound to div

    Saturday, March 9, 2019 8:50 PM
  • User475983607 posted
    The code posted works as expected. Share your code if you want someone to review it for correctness.
    Saturday, March 9, 2019 10:12 PM
  • User1922541779 posted

    Thanks my bad!!! I did not add e.preventdefault to my jq. after adding it it worked

    Sunday, March 10, 2019 2:59 PM
  • User475983607 posted

    kobosh

    I understand that you ran the code in your post  in visual studio and worked? if that is the case did you do any changes in chrome like allow java script?  Did you do anthing in the code behind?

    I'm using the default Chrome settings where JavaScript enabled.  There's no code behind.  The code posted above is all the code.

    kobosh

    my code is web form . I placed JQ  in the default page body. Also I tried placing it in Site master head. Both give the same result: the test.txt file is being accessed and read. I placed alert(data) in the onsuccess function and the alert show the test in test.txt file. But the data is not shown in the div.

    For the second time, share all your code or code that reproduces the issue if you want debugging assistance.    Keep in mind, that JavaScript runs in the browser not on the server where master pages and content pages run.  It is very important to understand the basics in order to design a solution.

    Unfortunately, I'm unable to decipher your source code.  Is there anyway you can post code that reproduces this issue?  Please use the code button in the toolbar.  It looks like {;}.

    Sunday, March 10, 2019 3:17 PM