dongzu3511 2014-08-13 23:56
浏览 39
已采纳

如何为$ .ajax生成PHP响应

A form is being submitted using the ajax code below. I am unsure what response to generate using PHP, so that $.ajax can call the appropriate callbacks done() and fail()

request = $.ajax({
        url: "php_process.php",
        type: "post",
        data: serializedData
    });

    // callback handler that will be called on success
    request.done(function (response, textStatus, jqXHR){
        // log a message to the console
        console.log("Hooray, it worked!");

    });

    // callback handler that will be called on failure
    request.fail(function (jqXHR, textStatus, errorThrown){
        // log the error to the console
        console.error(
            "The following error occured: "+
            textStatus, errorThrown
        );
    });`
  • 写回答

2条回答 默认 最新

  • dongya2029 2014-08-14 01:47
    关注

    See .done() and .fail() responses at result area , network tab at console

    $(function() {
        var urls = ["/echo/jsons/", "/echo/json/"];
        var request = function(url) {
    
            return $.ajax({
            url: url,
            type: "POST",
            data: {json : JSON.stringify({"abc":[123]}) }            
            });
        };           
        // callback handler that will be called on success
     $.each(urls, function(k, v) {
         $.when(request(v))
        .done(function (response, textStatus, jqXHR){
            // log a message to the console
            console.log("Hooray, it worked!", response);
            $("body").prepend("DONE: <br>" 
                              + Object.keys(response) + ":" 
                              +  response[Object.keys(response)] 
                              + "<br><br>")
        })    
        // callback handler that will be called on failure
       .fail(function (jqXHR, textStatus, errorThrown){
            // log the error to the console
            console.log("The following error occured: "
                        + textStatus, errorThrown);
           $("textarea")
           .before("FAIL: <br>")
           .val(jqXHR.getAllResponseHeaders() +"
    " 
               + jqXHR.status +"
    "+ textStatus 
               +"
    "+ errorThrown +"
    " +  jqXHR.responseText)
        });  
    });
    });
    

    jsfiddle http://jsfiddle.net/guest271314/L3jbvnex/1/

    See

    PHP: How to send HTTP response code?

    http://php.net/manual/en/function.http-response-code.php

    http://php.net/manual/en/function.header.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站