0

I am creating a web application in yii where I am sending ajax request to update the database.

The application worked for the first time and after that it threw the following error.

Here the data is getting updated in the database but I am not getting the response back.

POST http://localhost:8080/simplerAP/index.php?r=site/UpdateRates 500 (Internal Server Error)
k.cors.a.crossDomain.send   @   jQuery-2.1.4.min.js:4
n.extend.ajax   @   jQuery-2.1.4.min.js:4
saveRates   @   index.php?r=site/tariffcard:528
btn.onclick @   index.php?r=site/tariffcard:499

My JS code is as follows

<script type="text/javascript">

  function enableTextBox(regularId, urgentId, btnId) 
  {
    //alert(regularId + " " + urgentId + " " + btnId);
    document.getElementById(regularId).removeAttribute("disabled");
    document.getElementById(urgentId).removeAttribute("disabled");
    //document.getElementById(regularId).className = "form-control";
    //document.getElementById(urgentId).className = "form-control";
    var btnSpan = document.getElementById(btnId);
    while(btnSpan.firstChild)
    {
      btnSpan.removeChild(btnSpan.firstChild);
    }
    var btn = document.createElement("INPUT");
    btn.type = "button";
    btn.value = "Save";
    btn.className = "btn btn-primary";
    btn.id = btnId;
    btn.onclick = function(){ saveRates(this.id, document.getElementById(regularId).value, document.getElementById(urgentId).value)};
    btnSpan.appendChild(btn);
  }

  function saveRates(btnId, regularValue, urgentValue)
  {
    //alert(btnId + " " + regularValue + " " + urgentValue);
    var clothType = btnId.substring(0, btnId.indexOf("-"));
    //alert(clothType);
    var tariffCardType = btnId.substring(btnId.indexOf("-")+1, btnId.indexOf("-", btnId.indexOf("-")+1));
    //alert(tariffCardType);
    var jobTypeCode = btnId.substring(btnId.indexOf("-", btnId.indexOf("-")+1)+1, btnId.length);
    if(jobTypeCode == "I")
    {
      jobType = "iron";
    }
    if(jobTypeCode == "WI")
    {
      jobType = "washiron";
    }
    if(jobTypeCode == "D")
    {
      jobType = "drycleaning";
    }
    //alert(jobType);

    var newRegularRate = regularValue;
    var newUrgentRate = urgentValue;

    $.ajax
    (
      {
        url:'/simplerAP/index.php?r=site/UpdateRates',
        type:'post',
        data:{tarifftardtype:tariffCardType, jobtype:jobType, clothtype:clothType, newregularrate:newRegularRate, newurgentrate:newUrgentRate},
        success:function(data)
                {
                  document.getElementById('demo').innerHTML = data;
                },
        cache:false}
    );
  }


  </script>

2 Answers2

1

Error 500 has nothing to do with your JavaScript. As you can read, it's server error. Try to check method UpdateRates. Problem could be anywhere - bad params, etc.

Here you can find further info.

(EDIT: JQuery Ajax Post results in 500 Internal Server Error is possibly solving same problem.)

Community
  • 1
  • 1
Pavel Pája Halbich
  • 1,529
  • 2
  • 17
  • 22
0

You should probably use json or Jsonp datatype and also make sure that your php script have header('content-type: application/json; charset=utf-8') .

Check this link how to make a ajax request with PHP

http://www.geekality.net/2010/06/27/php-how-to-easily-provide-json-and-jsonp/