Leon van der Veen
Leon van der Veen

Reputation: 1672

jquery json request failed

I'm trying to make a json call with jquery but noting happened. My code:

javascript:

<script type="text/javascript" charset="utf-8">
$(document).ready(function() 
{
    $("#TwImport").click(function() 
    {
        $.ajax({
            type: "POST",
            url: "https://<?php echo $_conf['siteurl']; ?>/files/connect/import/customers.php",
            dataType: 'json',
            success: function (data) 
            {
                alert(data.percentage); 
            }
        });
    });
});
</script>

PHP

$output = array(
    'percentage' => "50"
);
echo json_encode($output);

Any suggestions?

Upvotes: 0

Views: 105

Answers (2)

rAjA
rAjA

Reputation: 919

The code looks fine to me,

EDITED Also try removing the protocol and use url: "//<?php echo $_conf['siteurl']; ?>/files/connect/import/customers.php",

$("#TwImport").click(function() 
    {
        $.ajax({
            type: "POST",
            url: "https://<?php echo $_conf['siteurl']; ?>/files/connect/import/customers.php",
            dataType: 'json',
            success: function (data) 
            {
                alert(data.percentage); 
            },
            error: function (jqXHR,textStatus,errorThrown)
            {
                //Check for any error here
            }
        });
    });

Upvotes: 1

Matt Indeedhat Holmes
Matt Indeedhat Holmes

Reputation: 725

if you add and error callback to the ajax call you should get some error printouts to let you know what is going on

$.ajax({
            type: "POST",
            url: "https://<?php echo $_conf['siteurl']; ?>/files/connect/import/customers.php",
            dataType: 'json',
            success: function (data) 
            {
                alert(data.percentage); 
            },
            error : function (e1, e2, e3) {
              console.log(e1);
              console.log(e2);
              console.log(e3);
            }


});

EDIT: i just had a thought, if i remember correctly jquery ajax doesnt like using full url's if possible try using a relative path

Upvotes: 0

Related Questions