weixin_33721344 2018-05-16 08:56 采纳率: 0%
浏览 20

有时Ajax呼叫失败

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?

展开全部

  • 写回答

1条回答 默认 最新

  • 乱世@小熊 2018-05-16 09:50
    关注

    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);
    
      }
    
    });
    
    评论
    编辑
    预览

    报告相同问题?

    悬赏问题

    • ¥15 PADS Logic 原理图
    • ¥15 PADS Logic 图标
    • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
    • ¥20 气象站点数据求取中~
    • ¥15 如何获取APP内弹出的网址链接
    • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
    手机看
    程序员都在用的中文IT技术交流社区

    程序员都在用的中文IT技术交流社区

    专业的中文 IT 技术社区,与千万技术人共成长

    专业的中文 IT 技术社区,与千万技术人共成长

    关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

    关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

    客服 返回
    顶部