AJAX PUT request to Phillips Hue Hub Local

I have created a file with the following:

<form id="myForm" name="myForm">
<div>
<button type="submit">Turn Light on</button>
</div>
</form>
<script> $('#myForm').submit(function(e){
e.preventDefault();
$.ajax({
    url:'http://192.168.1.102/api/user/lights/1/state',
    type:'PUT',
    data: JSON.stringify({"on":true}),
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(data){alert(data);},
    failure: function(errMsg) {
    alert(errMsg);
    }
});
});
</script>

Currently, no error or success message is displayed and the light does not turn on. I can only put it down to there being an error in my code, or am I misunderstanding Jquery & AJAX and they are not client side, hence why this doesn’t work?

EDIT: Typo on the form ID was the issue.

It is possible for user refresh webpage and redirect to another url?

I’m building a site wordpress and i want to redirect to a page
when user refresh, redirect home of my site.
It is possible?
this code can be right?

$(window).unload(function()
{
    var reload = confirm("are you sure you want to leave?");
    if(reload)
    {
        location.reload(); // Refresh the site, not needed really.
        location.href = "http://example.com"; // Redirect.
    }
});

AJAX Request Returns 404 with “select from” Anywhere in a Submitted Text Field [on hold]

A user is submitting a form using ajax with some notes that contain “select” and “from” organically in it, with many words in between them, and the server is returning a 404 error before even reaching the PHP script. Anybody seen this before or aware of any server-side SQL injection checks that would be occurring?

ajax – lost query results [on hold]

(ok. Im presenting the whole code so that people may finally see what it is about) Im new to AJAX. I have created a ajax-generated query in computadores.php
in which there is a pagination that shows different pages of the query results (1, 2, 3, etc). it comes from the superfun function. i use the listcomp.php file. Now, I want to able to acess those pages when i click in each number in the pagination. But when i click, the query results are gone. I have created a new AJAX function to replicate the query when i click in a number (function superpag), but even so the results are gone in computadores.php.

And now that i mention computadores.php, i would like to know if there is a way to disable the sql query that is in computadores.php (gives total results). I only need it when the screen is loaded. When i start using the ajax querys, i want it disabled. Otherwise, when i click a number in the ajax-generated numeration, it goes back to this (inicial) query. thats why that this code is disabled right now (which also means that when i load the page, no results appear, something that i dont want).

This is the JS code

var inpreco=["","","","","","","","","","","",""];
var altpreco=["","","","","","","","","","","",""];
var inprocess=["","","","","","","","","","","",""];
var altprocess=["","","","","","","","","","","",""];
var inpram=["","","","","","","","","","","",""];
var altram=["","","","","","","","","","","",""];
var inpdisco=["","","","","","","","","","","",""];
var altdisco=["","","","","","","","","","","",""];
var compcount1=0;
var compcount2=0;
var compcount3=0;
var compcount4=0;

$(".comppreco").click(function({compcount1=SuperFun(this,"#preco",inpreco,altpreco,"compind1","compactiv1",compcount1,"preco","1 AND 5000","listacomput.php",
"preco","process","ram","disco","preco","preco","preco");});
$(".compprocess").click(function(){compcount2=SuperFun(this, "#process",inprocess,altprocess,"compind2","compactiv2",compcount2,"processador_id","1 AND 16", "listacomput.php","preco","process","ram","disco","preco","preco","preco");});

$(".compram").click(function(){compcount3=SuperFun(this,"#ram",inpram,altram,"compind3","compactiv3",compcount3,"RAM","1 AND 64","listacomput.php", "preco",
"process","ram","disco","preco","preco","preco");});

$(".compdisco").click(function(){compcount4=SuperFun(this,"#disco",inpdisco,altdisco,"compind4","compactiv4",compcount4,"disco","1 AND 2256", "listacomput.php","preco","process","ram","disco","preco","preco","preco");});

function SuperFun(element,input,inpArray,secArray,secIndex,inpActive,counter,msqlip,ending,ajax,input1,input2,input3,input4,input5,input6,input7){
var inpValue=$("#"+element.id+"i").val();
var sIndex=$("#"+element.id+"i").data(secIndex);
var inpa=$("#"+element.id+"i").data(inpActive);
var secArray2=secArray.filter(Boolean);
if (counter==0){
  counter++;
  $("#"+element.id+"i").data(inpActive, "primary");
  inpArray[0]=(inpValue);  
}else
if (inpa=="no") {
  counter++;
  $("#"+element.id+"i").data(inpActive, "yes");
  inpArray[sIndex]=(" OR "+msqlip+" BETWEEN "+inpValue);
  secArray[sIndex]=(sIndex);
}else
if (inpa=="yes") {
  counter--;
  $("#"+element.id+"i").data(inpActive, "no");
  inpArray[sIndex]="";
  secArray[sIndex]="";
}else
if (inpa=="primary" && counter!==1) {
  counter--;
  $("#"+element.id+"i").data(inpActive, "no");
  inpArray[0]=$("input[data-"+secIndex+"*="+secArray2[0]+"]").val();
  inpArray[$("input[data-"+secIndex+"*="+secArray2[0]+"]").data(secIndex)]="";
  $("input[data-"+secIndex+"*="+secArray2[0]+"]").data(inpActive, "primary")
  secArray[$("input[data-"+secIndex+"*="+secArray2[0]+"]").data(secIndex)]="";    
} else
if (inpa=="primary" && counter==1) {
  counter--;
  $("#"+element.id+"i").data(inpActive, "no");
  inpArray[sIndex]="";
  inpArray[0]=ending;   
}
$(input).val(inpArray[0]+inpArray[1]+inpArray[2]+inpArray[3]+inpArray[4]+inpArray[5]+inpArray[6]+inpArray[7]+inpArray[8]+inpArray[9]+inpArray[10]+
  inpArray[11]);
$.post(ajax+"?"+input1+"="+$("#"+input1).val()+"&"+input2+"="+$("#"+input2).val()+"&"+input3+"="+$("#"+input3).val()+"&"+input4+"="+$("#"+input4).val()+
"&"+input5+"="+$("#"+input5).val()+"&"+input6+"="+$("#"+input6).val()+"&"+input7+"="+$("#"+input7).val(), function(data) {
  $("#paramount").html(data);
});
if (counter==0) {
  $("#"+secIndex).html("");  
}else {
  $("#"+secIndex).html(counter);
}
return counter;
};


$(".comppag").click(function(){SuperPag("listacomput.php","preco","process","ram","disco");});

function SuperPag(ajax,input1,input2,input3,input4){
$.post(ajax+"/?"+input1+"="+$("#"+input1).val()+"&"+input2+"="+$("#"+input2).val()+"&"+input3+"="+$("#"+input3).val()+"&"+input4+"="+$("#"+input4).val(), function(data) {
  $("#paramount").html(data);
});
};

This is the listcomp.php code:

<?php

$pagina=1;
if (empty($_GET["pagina"]) || $_GET["pagina"] < 1) {$pagina = 1;}else {$pagina = intval($_GET["pagina"]);}
$quantidade = 3;
$offset = ($pagina - 1) * $quantidade;
$preco=mysqli_real_escape_string($conn, $_GET["preco"]);
$process=mysqli_real_escape_string($conn, $_GET["process"]);
$ram=mysqli_real_escape_string($conn, $_GET["ram"]);
$disco=mysqli_real_escape_string($conn, $_GET["disco"]);
$sql = "SELECT SQL_CALC_FOUND_ROWS computadores.id, computadores.nome, computadores.preco, processadores.velocidade AS velocidade, computadores.RAM, computadores.disco FROM computadores LEFT JOIN processadores ON computadores.processador_id=processadores.id WHERE (preco BETWEEN $preco) AND (processadores.velocidade BETWEEN $process) AND (RAM BETWEEN $ram) AND (disco BETWEEN $disco) LIMIT $offset,$quantidade";
$result = mysqli_query($conn, $sql);
echo mysqli_error($conn);
$sql = "SELECT FOUND_ROWS()";
$contagem = mysqli_query($conn, $sql);
$total_resultados = mysqli_fetch_row($contagem);
$total_resultados = $total_resultados[0];
$ultima_pagina = ceil($total_resultados / $quantidade);
$link = "&quantidade=$quantidade";
if (mysqli_num_rows($result)>0) {$row = mysqli_fetch_assoc($result);
echo "
        <li class='entrada'>
            <img src='imagens/computadores/$row[id].JPG'>
            <div class='entradanome'>$row[nome]</div>
            <div class='entradacarac'>
                Velocidade: $row[velocidade] GHz<br>
                RAM: $row[RAM] GB<br>
                Disco: $row[disco] GB
            </div>
            <div class='entradapreco'>$row[preco]€</div>
        </li>";
while($row = mysqli_fetch_array($result)) {
    echo "
        <li class='entrada'>
            <img src='imagens/computadores/$row[id].JPG'>
            <div class='entradanome'>$row[nome]</div>
            <div class='entradacarac'>
                Velocidade: $row[velocidade] GHz<br> 
                RAM: $row[RAM] GB<br>  
                Disco: $row[disco] GB
            </div>
            <div class='entradapreco'>$row[preco]€</div>
        </li>";
}
echo "$total_resultados";
echo "<div class='ajpaginacao'>";

if ($pagina > 1) {
    echo "<a href='?pagina=" . ($pagina - 1) . "$link' class='comppag'><span >&lt;</a>";
}else {
    echo "<span>&lt;</span>";
}
for ($i = 1; $i <= $ultima_pagina; $i++){
    if ($i == $pagina) {
        echo "<span class=\"active\" comppag>$i</span>";
    }else {
        echo "<a href='?pagina=$i$link' class='comppag'>$i</a>";
    }
}
if ($pagina < $ultima_pagina) {echo "<a href='?pagina=" . ($pagina + 1) . "$link' class='comppag'>&gt;</a>";}else {echo "<span>&gt;</span>";}

echo "</div>";
}else {
echo "";
}mysqli_close($conn);
?>

This is the computadores.php file

<div class="areaprinc">
<ul id="paramount">
    <?php
    $pagina=1;
    if (empty($_GET["pagina"]) || $_GET["pagina"] < 1) {$pagina = 1;}else {$pagina = intval($_GET["pagina"]);}
    $quantidade = 4;
    $offset = ($pagina - 1) * $quantidade;
    $sql = "SELECT SQL_CALC_FOUND_ROWS computadores.id, computadores.nome, computadores.preco, processadores.velocidade AS velocidade, computadores.RAM, computadores.disco FROM computadores LEFT JOIN processadores ON computadores.processador_id = processadores.id LIMIT $offset,$quantidade";
    $result = mysqli_query($conn, $sql);
    echo mysqli_error($conn);
    $sql = "SELECT FOUND_ROWS()";
    $contagem = mysqli_query($conn, $sql);
    $total_resultados = mysqli_fetch_row($contagem);
    $total_resultados = $total_resultados[0];
    $ultima_pagina = ceil($total_resultados / $quantidade);
    $link = "&quantidade=$quantidade";
    $computadores = array();
    if (mysqli_num_rows($result)>0) {
        $row = mysqli_fetch_assoc($result);
        $row = mysqli_fetch_assoc($result);
        if (mysqli_num_rows($result) > 1) {echo mysqli_num_rows($result) . " computadores<br>";}
        elseif (mysqli_num_rows($result) == 1) {echo mysqli_num_rows($result) . " computador<br>";}
        echo "
        <li class='entrada'>
            <img src='imagens/computadores/$row[id].JPG'>
            <div class='entradanome'>$row[nome]</div>
            <div class='entradacarac'>
                Velocidade: $row[velocidade] GHz<br> 
                RAM: $row[RAM] GB<br>  
                Disco: $row[disco] GB
            </div>
            <div class='entradapreco'>$row[preco]€</div>
        </li>";
    while($row = mysqli_fetch_array($result)) {
        echo "
        <li class='entrada'>
            <img src='imagens/computadores/$row[id].JPG'>
            <div class='entradanome'>$row[nome]</div>
            <div class='entradacarac'>
                Velocidade: $row[velocidade] GHz<br> 
                RAM: $row[RAM] GB<br>  
                Disco: $row[disco] GB
            </div>
            <div class='entradapreco'>$row[preco]€</div>
        </li>";
        }
    echo "<div class='paginacao'>";
    //if ($pagina > 1) {echo "<a href='?pagina=" . ($pagina - 1) . "$link'>&lt;</a>";}else {echo "<span>&lt;</span>";}
    //for ($i = 1; $i <= $ultima_pagina; $i++){if ($i == $pagina) {echo "<span class=\"active\">$i</span>";}else {echo "<a href='?pagina=$i$link'>$i</a>";}}
    //if ($pagina < $ultima_pagina) {echo "<a href='?pagina=" . ($pagina + 1) . "$link'>&gt;</a>";}else {echo "<span>&gt;</span>";}
    echo "</div>";
    }
    ?>
</ul>
<ul id="filtpesq">

    <li>PESQUISA<br>
        <span>Filtros</span>

        <ul><div class="t">Preço</div><div id="compind1" class="incount t"></div>
            <input id="preco" type="hidden" name="preco" value='1 AND 5000'>
            <li class="input">
                <input id="op1i" class="css-checkbox" type="checkbox" value="201 AND 400" data-compind1="1" data-compactiv1="no"/>
                <label id="op1" class="comppreco css-label" for="op1i">€201 - €400</label>
            </li>
            <li class="input">
                <input id="op2i" class="css-checkbox" type="checkbox" value="401 AND 600" data-compind1="2" data-compactiv1="no"/>
                <label id="op2" class="comppreco css-label" for="op2i">€401 - €600</label>
            </li>
            <li class="input">
                <input id="op3i" class="css-checkbox" type="checkbox" value="601 AND 800" data-compind1="3" data-compactiv1="no"/>
                <label id="op3" class="comppreco css-label" for="op3i">€601 - €800</label>
            </li>   
            <li class="input">
                <input id="op4i" class="css-checkbox" type="checkbox" value="801 AND 1000" data-compind1="4" data-compactiv1="no"/>
                <label id="op4" class="comppreco css-label" for="op4i">€801 - €1000</label>
            </li>
            <li class="input">
                <input id="op5i" class="css-checkbox" type="checkbox" value="1001 AND 1500" data-compind1="5" data-compactiv1="no"/>
                <label id="op5" class="comppreco css-label" for="op5i">€1001 - €1500</label>
            </li>
            <li class="input">
                <input id="op6i" class="css-checkbox" type="checkbox" value="1501 AND 2000" data-compind1="6" data-compactiv1="no"/>
                <label id="op6" class="comppreco css-label" for="op6i">€1501 - €2000</label>
            </li>
            <li class="input">
                <input id="op7i" class="css-checkbox" type="checkbox" value="2001 AND 2500" data-compind1="7" data-compactiv1="no"/>
                <label id="op7" class="comppreco css-label" for="op7i">€2001 - €2500</label>
            </li>
            <li class="input">
                <input id="op8i" class="css-checkbox" type="checkbox" value="2501 AND 5000" data-compind1="8" data-compactiv1="no"/>
                <label id="op8" class="comppreco css-label" for="op8i">€2501 - €5000</label>
            </li>   
        </ul>
    </li>
    <li>
        <ul>
            <div class="t">Velocidade</div><div id="compind2" class="incount t"></div>
            <input id="process" type="hidden" name="process" value='1 AND 4.2'>
            <li class="input">
                <input id="op9i" class="css-checkbox" type="checkbox" value="1.50 AND 2.00" data-compind2="1" data-compactiv2="no"/>
                <label id="op9" class="compprocess css-label" for="op9i">1.5 GHz - 2 GHz</label>
            </li>
            <li class="input">  
                <input id="op10i" class="css-checkbox" type="checkbox" value="2.50 AND 3.00" data-compind2="2" data-compactiv2="no"/>
                <label id="op10" class="compprocess css-label" for="op10i">2.5 GHz - 3 GHz</label>
            </li>
            <li class="input">
                <input id="op11i" class="css-checkbox" type="checkbox" value="3.10 AND 3.50" data-compind2="3" data-compactiv2="no"/>
                <label id="op11" class="compprocess css-label" for="op11i">3.1 GHz - 3.5 GHz</label>
            </li>   
            <li class="input">
                <input id="op12i" class="css-checkbox" type="checkbox" value="3.60 AND 3.90" data-compind2="4" data-compactiv2="no"/>
                <label id="op12" class="compprocess css-label" for="op12i">3.6 GHz - 3.9 GHz</label>
            </li>
            <li class="input">
                <input id="op13i" class="css-checkbox" type="checkbox" value="4.00 AND 4.20" data-compind2="5" data-compactiv2="no"/>
                <label id="op13" class="compprocess css-label" for="op13i">4 GHz - 4.2 GHz</label>
            </li>
        </ul>
    </li>
    <li>
        <ul>
            <div class="t">Ram</div><div id="compind3" class="incount t"></div>
            <input id="ram" type="hidden" name="ram" value='1 AND 2256'>
            <li class="input">
                <input id="op20i" class="css-checkbox" type="checkbox" value="2 AND 2" data-compind3="1" data-compactiv3="no"/>
                <label id="op20" class="compram css-label" for="op20i">2 GB</label>
            </li>
            <li class="input">
                <input id="op21i" class="css-checkbox" type="checkbox" value="4 AND 4" data-compind3="2" data-compactiv3="no"/>
                <label id="op21" class="compram css-label" for="op21i">4 GB</label>
            </li>
            <li class="input">
                <input id="op22i" class="css-checkbox" type="checkbox" value="8 AND 8" data-compind3="3" data-compactiv3="no"/>
                <label id="op22" class="compram css-label" for="op22i">8 GB</label>
            </li>   
            <li class="input">
                <input id="op23i" class="css-checkbox" type="checkbox" value="12 AND 12" data-compind3="4" data-compactiv3="no"/>
                <label id="op23" class="compram css-label" for="op23i">12 GB</label>
            </li>
            <li class="input">
                <input id="op24i" class="css-checkbox" type="checkbox" value="16 AND 16" data-compind3="5" data-compactiv3="no"/>
                <label id="op24" class="compram css-label" for="op24i">16 GB</label>
            </li>
            <li class="input">
                <input id="op25i" class="css-checkbox" type="checkbox" value="32 AND 32" data-compind3="6" data-compactiv3="no"/>
                <label id="op25" class="compram css-label" for="op25i">32 GB</label>
            </li>
            <li class="input">
                <input id="op26i" class="css-checkbox" type="checkbox" value="64 AND 64" data-compind3="7" data-compactiv3="no"/>
                <label id="op26" class="compram css-label" for="op26i">64 GB</label>
            </li>
        </ul>
    </li>
    <li>
        <ul>
            <div class="t">Disco</div><div id="compind4" class="incount t"></div>
            <input id="disco" type="hidden" name="disco" value='1 AND 2512'>
            <li class="input">
                <input id="op27i" class="css-checkbox" type="checkbox" value="256 AND 256" data-compind4="1" data-compactiv4="no"/>
                <label id="op27" class="compdisco css-label" for="op27i">256 GB</label>
            </li>
            <li class="input">  
                <input id="op28i" class="css-checkbox" type="checkbox" value="500 AND 500" data-compind4="2" data-compactiv4="no"/>
                <label id="op28" class="compdisco css-label" for="op28i">500 GB</label>
            </li>
            <li class="input">
                <input id="op29i" class="css-checkbox" type="checkbox" value="1000 AND 1000" data-compind4="3" data-compactiv4="no"/>
                <label id="op29" class="compdisco css-label" for="op29i">1 TB</label>
            </li>   
            <li class="input">
                <input id="op30i" class="css-checkbox" type="checkbox" value="1120 AND 1120" data-compind4="4" data-compactiv4="no"/>
                <label id="op30" class="compdisco css-label" for="op30i">1 TB + 120 GB</label>
            </li>
            <li class="input">
                <input id="op31i" class="css-checkbox" type="checkbox" value="1128 AND 1128" data-compind4="5" data-compactiv4="no"/>
                <label id="op31" class="compdisco css-label" for="op31i">1 TB + 128 GB</label>
            </li>
            <li class="input">
                <input id="op32i" class="css-checkbox" type="checkbox" value="1256 AND 1256" data-compind4="6" data-compactiv4="no"/>
                <label id="op32" class="compdisco css-label" for="op32i">1 TB + 256 GB</label>
            </li>
            <li class="input">
                <input id="op33i" class="css-checkbox" type="checkbox" value="2256 AND 2256" data-compind4="7" data-compactiv4="no"/>
                <label id="op33" class="compdisco css-label" for="op33i">2 TB + 256 GB</label>
            </li>
        </ul>
    </li>
</ul>

unable to hit .done funtion in ajax

This is my ajax code. My controller method is hitting but it is not going to .done function.

 $.ajax('/Account/ChangePassword', {
                "data": $form.serialize() + "&userId=" + $scope.data.LoginResponce.UserId,
                "type": "POST"
            })
            .done(function (data, textStatus, jqXHR) {
                if (data.Result) {
                    $("#ChangePasswordModel").modal('hide');
                    bootbox.alert(data.Message + " Please login with new credentials");
                    $('#change-password-form')[0].reset();
                    $('#change-password-form').data('formValidation').resetForm();
                    $(".password-progress").addClass("hide");
                }
                else {
                    $scope.data.ChangePasswordResponceMessage = 'Unable to change the password now. ' + data.Message;
                    $scope.$apply();
                }
            })
            .fail(function (jqXHR, textStatus, errorThrown) {
                $scope.data.ChangePasswordResponceMessage = 'Some unexpected error occurred. ' + errorThrown;
                $scope.$apply();
            })
            .always(function (jqXHR, textStatus, errorThrown) {
                waitingDialog.hide();
            });

Multiple AJAX calls are causing the server to hang, for exactly sixty seconds

I have a door designer that loads in options based on what the user picks. When the page the designer sits on initially loads it makes a series of AJAX calls (25 in total one for each option) to obtain JSON objects that contain lists of choices for each option.

My problem is that the AJAX calls hang, for about a minute, almost every time. This is what Chrome’s network panel shows:

enter image description here

I do not understand why the first six calls hang. I know that there is a limit of six calls from a browser at a time, but as each resolves the next query should be passed until they are all complete?

The call is made from a JS file, inside a document ready function which aims at (for example) http://website.name/api.php/api/products/get/door_colour

This call hits the api.php page and is split into three components, the page/class that handles the query (products), the function name (get) and the thing we’re looking for (door_colour).

if (session_status() == PHP_SESSION_NONE) {
    session_start();
}

// Include Core Classes
foreach (glob("classes/*.php") as $filename) {
    include($filename);
}

// Get URL Segments
$url = $_SERVER["REQUEST_URI"];
$url = str_replace("api.php","",$url);
$url = str_replace("//","",$url);

// Split Segments
$segments = explode("/",$url);

$root = $segments[0];
$class = $segments[1];
$method = $segments[2];

// Create Parameters Array
$parameters = $segments;
unset($parameters[0]);
unset($parameters[1]);
unset($parameters[2]);

// Rebase Array
$parameters = array_values($parameters);

$instance = new $class($parameters);
$output = $instance->$method();

$json = json_encode($output);

echo $json;

The products.php file is too long to post in it’s entirety but essentially it boils down to:

public function get($option)
{
    //query the db
    $results = pdo::query('select * where option = :option');

    //create an empty array to be returned as json later
    $data = array();

    //loop through the results
    foreach($results as $result){
        //check if there is an image for the option
        if(file_exists($result->filename)){
            //add the option to the data to be returned
            $data[] = $results
        }
    }
    //echo the data as json
    echo json_encode($data);
}

I’m not able to see anything that might account for the initial hang. The only thing I have come across so far is PHP session locks as the api.php file does start a session each time, which is not closed that I can see, but the session isn’t actually used at all so I’m not convinced.

If I use Chromes network panel to ‘replay XHR’ each of the original six that hangs returns instantly. The amount of data returned is tiny, 15KB at most.

What could be causing the AJAX calls to hang?

How do I return the response from an asynchronous call?

I have a function foo which makes an Ajax request. How can I return the response from foo?

I tried to return the value from the success callback as well as assigning the response to a local variable inside the function and return that one, but none of those ways actually return the response.

function foo() {
    var result;

    $.ajax({
        url: '...',
        success: function(response) {
            result = response;
            // return response; // <- I tried that one as well
        }
    });

    return result;
}

var result = foo(); // It always ends up being `undefined`.

Can ‘echo’ be used in php-file that receives an ajax message

So far I have been using AJAX requests successfully in the following way:

 function JsErrorReport(stack,anm, fehler, betreff ) {
    var jqXHR = $.ajax ({
        url:    "ajax/Test.php",  
        type:   "POST",
        data:   {
            Stack : stack,
            Anmerkung : anm,
            Fehler: fehler,
            Betreff: betreff
        },
        dataType:   "json"
    })
    .done(function(data) {
        if(data.success) {
            console.log("ErrorReportJs()- data.success: " + data.success);
            alert("ErrorReportJs()- data.success: " + data.success);
        } else {
            console.log("data.error: " + data.error); 
            alert("data.error: " + data.error);
        }
    })
    .fail (function(jqXHR, textStatus, errorThrown) {
        console.log(".fail JsErrorReport: " + errorThrown);
        alert(".fail JsErrorReport: \n" + errorThrown  + jqXHR.responseText);
    });

}

The php script typically looks like this:

<?php

...

header('Content-Type: application/json');   // Set the json header content-type 
echo json_encode($data);                    // and output the $json string so that it can be    
                                        //handled by our javascript code as a json object. 
?>

The problem is that in the php-script I must not use the ‘echo’ command for testing, It causes a “SyntaxError: Unexpected token …”.
Is there another way of making an ajax request that allows me to use the ‘echo’ command in the php-scipt?

Also “fwrite($fd, $msg)” cannot be used to log some error data. The script reaches the alert() function and restarts over and over again.
Is there a way to circumvent this behaviour?


Edit:
This is the complete (reduced) file addressed by the AJAX request. fwrite() causes the trouble.

<?php
  $fd = fopen(dirname(__FILE__) . "/../log/error.log", "at"); 
  $msg = "MeineMessage\n";
  if($fd){  
     fwrite($fd, $msg);  
     fclose($fd);   
  }
  $data["success"] = true;
  header('Content-Type: application/json'); 
  echo json_encode($data);  
?>

Success message displaying on the wrong page

I am new to Ajax and I am trying CRUD operations using the same. I have an index.php page where I can insert and display data, also I have links to update and delete data over there.Create, Read, Update are working fine. Now the problem is that when I click the link to Delete, I am successfully sending the id to the delete_back.php and the value is getting deleted but the success message which should be printed on the index.php as I have a div named #delete_msg over there but somehow the success message is being printed on delete_back.php.

index.php

<div id="delete_msg"></div>

select_back.php

    <?php
include("config.php");

$data=mysqli_query($con,"select * from student");
$col=mysqli_num_fields($data);
echo "<table>";
while($row=mysqli_fetch_array($data))
{
    echo "<tr>";
    for($i=0;$i<$col;$i++)
    {
        echo "<td>".$row[$i]."</td>";   
    }   
    echo "<td><a href='update_front.php?id=$row[0]&nm=$row[1]&add=$row[2]&cont=$row[3]'> Update </a></td>";
    echo "<td><a class='delete' href='delete_back.php?id=$row[0]'>Delete</a></td>";
    echo "</tr>";
}
echo "</table>";
?>

delete_back.php

<?php
$id=$_GET['id'];

include("config.php");
$data=mysqli_query($con,"delete from student where id='$id'");
if($data=="true")
{
    echo "Data Deleted";    
}
else
{
    echo "Delete Error";    
}
?>

ajax file

$(".delete").click(function(event){
        event.preventDefault();
        $.ajax({
            url:"delete_back.php",
            dataType:"html",
            success:function(msgStr){
                $("#delete_msg").html(msgStr);
        }
    })
  })

understanding network tab on chrome console

I’m in the process of writing an app that builds a table of Trello card data based on multiple API calls, and while the app works I’m finding the performance degrades considerably the longer it runs. The initial calls take a couple of seconds while later calls (after 100 runs or so) take upwards of a minute.

Looking at the XHR Network tab on my Chrome console, I can see the bulk of the call is taken by the ‘Content Download’ phase of the Ajax call. I’m curious as to whether this means the issue is with my application or if the problem resides with the API I’m trying to call? I’m a bit of a novice so my terminology is probably not appropriate here.

enter image description here