两个请求而不是一个在凤凰应用程序中的Ajax调用中

时间:2015-09-17 23:31:50

标签: ajax phoenix-framework

我只通过一次调用得到了两个不同的请求(单击HTML元素),这就是在第二个请求上生成错误,如下所示:

[info] PUT /editables/Pag6
[debug] Processing by Skeleton.EditableController.update/2
  Parameters: %{"editable" => %{"content" => "<p>Oá</p>"}, "par" => "Pag6"}
  Pipelines: [:browser, :browser_session]
[debug] SELECT e0."id", e0."title", e0."content", e0."inserted_at", e0."updated_at" FROM "editables" AS e0 WHERE (e0."title" = $1) ["Pag6"] OK query=154.2ms queue=15.2ms
[debug] BEGIN [] OK query=0.4ms
[debug] UPDATE "editables" SET "content" = $1, "updated_at" = $2 WHERE "id" = $3 ["<p>Oá</p>", {{2015, 9, 17}, {23, 15, 17, 0}}, 9] OK query=7.5ms
[debug] COMMIT [] OK query=7.4ms
[info] Sent 302 in 309ms
[info] PUT /editables/9
[debug] Processing by Skeleton.EditableController.update/2
  Parameters: %{"editable" => %{"content" => "<p>Oá</p>"}, "par" => "9"}
  Pipelines: [:browser, :browser_session]
[debug] SELECT e0."id", e0."title", e0."content", e0."inserted_at", e0."updated_at" FROM "editables" AS e0 WHERE (e0."title" = $1) ["9"] OK query=0.7ms
[info] Sent 404 in 42ms
[error] #PID<0.301.0> running Skeleton.Endpoint terminated
Server: localhost:4000 (http)
Request: PUT /editables/9
** (exit) an exception was raised:
    ** (Ecto.NoResultsError) expected at least one result but got none in query:

from e in Skeleton.Editable,
  where: e.title == ^"9"

第一个请求是Ok,并且“par”参数具有正确的“Pag6”值,但在第二个请求中“par”有错误(它让我回想起id)“9”值。 Ajax是:

$("#save").click(function() {
  var content = $("#editor").html();  
  $.ajax({
    url: "/editables/Pag6",     //URL do controlador para a função que quero usar, neste caso PUT para fazer Update (4 é a ID da row)
    type: "put",
    data: {
      editable: { content: content,
//                  title: "pageN"
      }
    },
    headers: {
        "X-CSRF-TOKEN": csrf 
    },
    dataType: "json",
    complete: function () {
      alert( "Data Loaded");
    }
  });
});

我无法理解为什么会这样: 1)两个请求而不是一个; 2)第二个请求中错误的“par”参数值...

1 个答案:

答案 0 :(得分:1)

要查找第二个Ajax请求的来源,请在Chrome开发工具中保留一个XHR断点,用于&#34;任何XHR&#34;。

触发XHR时,执行将暂停。您可以返回堆栈跟踪以查找实际触发请求的代码。 enter image description here