Ajax Call Fails Sometimes

Mmh Source

I have an ajax call which is triggered by a button click but even though everything looks good, it sometimes stops working. The error is 500 Internal Server which is pretty broad so I tried to look for the error inside my function but no luck so far. The Is there anything wrong with the code?

Here's my AJAX Call:

jQuery(document).ready( function($){

$('.modal-footer').on('click', '#tempsave_submit', function() {
        //Loads all the variables to be sent    
        var source = $("#source-save").text();
        var name = $("input[id='pname-save']").val();
        var stage = $("input[id='pstage-save']").val();

        //Checks to see if any of those variables are empty
        if (source == null || name == null || stage == null)
            {$("#failstat").text("Error Reference Number: 300");
            $("#failsave").slideDown(100).delay(2000).slideUp(100);
            }

        //If they are all valid, it calls the ajax function
    else{
        $.ajax({
        url: 'https://website.com/tempsave',
        type: 'POST',
        data: {
            source_save: source,
            pname_save: name,
            pstage_save: stage

        },
        dataType : 'html',
        timeout: 30000,
        cache: false,
        success: function () {


            $("#successsave").slideDown(100).delay(2000).slideUp(100);

        },
        error: function (jqXHR, exception) {

            $("#failstat").text("Error Reference Number: 343-" + jqXHR.status + exception);
            $("#failsave").slideDown(100).delay(2000).slideUp(100);

        }

    });
    }
    });

    });

And here's the target php:

if($_SERVER["REQUEST_METHOD"] == "POST")
{
    $source = $_POST["source_save"];
    $name = temp_input_check($_POST["pname_save"]);
    $description = temp_input_check($_POST["pstage_save"]);

    // tempsave_write() is a predefined function-no issues with this part
    $fid = tempsave_write($source, $name);

    // tempsave_save() is a predefined function-no issues with this part
    $query = tempsave_save($name , $description , $fid);
}else{
    $html = '
  <h2>No Projects Were Found!</h2>
  </div>
  <p>Start by creating a new project. Your projects will be displayed here 
  once you submit them.</p>';
    return $html;
}

I checked the response when it gives me 500 Internal Error, and it was the html returned from 'else'. This means that the server didn't send it as POST method in the first place. Is that possible. @Taplan helped me and we found out that the method was GET! How's this possible?

UPDATE: I had to change some parts of my html code (didn't touch the codes related to ajax call) and now ajax is only sending it via GET method, hence, gives 500 error. One of the parameters that is being sent is html source code which was changed and now it sends as GET. I tried to narrow it down and found out that the parameters are not even available as GET even though the page says they were sent as GET. Right now I am sure that the issue is due to the html source code being sent but don't know what's wrong with it. Any help?

phpjqueryajaxpostdrupal-7

Answers

answered 4 months ago Isaac Boakye #1

The only thing I see wrong is using "type" in your ajax call block instead of "method". See here: http://api.jquery.com/jquery.ajax/

Your code should look like this:

$.ajax({
  url: 'https://website.com/tempsave',
  method: 'POST',
  data: {
    source_save: source,
    pname_save: name,
    pstage_save: stage

  },
  dataType : 'html',
  timeout: 30000,
  cache: false,
  success: function (response) {
    $("#successsave").slideDown(100).delay(2000).slideUp(100);

  },
  error: function (jqXHR, exception) {        
    $("#failstat").text("Error Reference Number: 343-" + jqXHR.status + exception);
    $("#failsave").slideDown(100).delay(2000).slideUp(100);

  }

});

comments powered by Disqus