• Resolved Invizion

    (@invizion)


    Hello,

    I’m trying to make a simple plugin to get data from website, source code. My problem is with ajax call that everytime is activated, it refreshes the page. Already tried e.preventdefault, return false, but nothing works. Maybe I’m doing something wrong. Could please someone point me?

    JS:

    $(function() {
        $("#bc_btn").click( function(e)
             {
                e.preventDefault();
                if($('#bc_input').val() == ''){
                    $("#bgworker").toggleClass('error');
                    $('#bgworker').text('Please insert a link.');
                }else{
                    $("#bgworker").toggleClass('working');
                    $('#bgworker').text('We are working...');
    
                    //get val from input
                    var url = $('#bc_input').val();
    
                    // get url to scrap
                    $.ajax({
                        data: {
                            action: 'getdatafromurl',
                            value: url
                        },
                        type: 'POST',
                        success: function(data) {
                            $('#bgworker').html(data).text();                  
    
                        },
                        error: function (jqXHR, exception) {
                        var msg = '';
                        if (jqXHR.status === 0) {
                            msg = 'Not connect.\n Verify Network.';
                        } else if (jqXHR.status == 404) {
                            msg = 'Requested page not found. [404]';
                        } else if (jqXHR.status == 500) {
                            msg = 'Internal Server Error [500].';
                        } else if (exception === 'parsererror') {
                            msg = 'Requested JSON parse failed.';
                        } else if (exception === 'timeout') {
                            msg = 'Time out error.';
                        } else if (exception === 'abort') {
                            msg = 'Ajax request aborted.';
                        } else {
                            msg = 'Uncaught Error.\n' + jqXHR.responseText;
                        }
                        $('#bgworker').html(msg);
                    },
                    });
                }
             }
    
        );
    
    });

    Function at default plugin php file:

    function specialgatherer(){
        if(isset($_POST['value'])){
            echo file_get_contents($_POST['value']);
        }else{
            echo "something went wrong";
        }
    }
    
    add_action ( 'wp_ajax_' . 'specialgatherer', 'getdatafromurl' );

    My Simple form:

    <form class="form-inline">
      <label for="bc_input">Link:</label>
      <input type="text" id="bc_input" name="bc_input" style="width:80%;">
      <button type="submit" id="bc_btn" name="bc_btn" onsubmit="return false">Do it!</button> 
    </form>
    <p id="bgworker" name="bgworker"></p>

    Hope someone could help me.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Dion

    (@diondesigns)

    If you change this line:

    <button type="submit" id="bc_btn" name="bc_btn" onsubmit="return false">Do it!</button>
    

    to this:

    <button type="button" id="bc_btn" name="bc_btn">Do it!</button>

    you should be able to eliminate the refresh.

    Thread Starter Invizion

    (@invizion)

    Unfortunately, your solution keeps refreshing the page for me.

    Thread Starter Invizion

    (@invizion)

    I’ve got it.
    Added the link to ajax.php as url and it worked. Now I can make my json requests.

    For future information in case someone need it:

                    $.ajax({
                        type: 'POST',
                        dataType: 'json',
                        url: '/wp-admin/admin-ajax.php',
                        data: {
                            action: 'call_udemygrab',
                            value: url
                        },
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Problem with ajax call into wordpress plugin’ is closed to new replies.