【问题标题】:Slim Framework Ajax jQuery PHPSlim 框架 Ajax jQuery PHP
【发布时间】:2015-06-24 19:25:28
【问题描述】:

我有一个个人资料页面,登录用户可以在其中发送好友请求。这是一个(树枝)视图:

{% extends 'templates/default.php' %}

{% block title %}{{ user.getFullNameOrUsername }}{% endblock %}

{% block content %}
<h2>{{ user.username }}</h2>
{% if auth %}
<input type="submit" value="Send Friend Request" id="sfr">
<br>
{% endif %}
<img src="" alt="Profile picture for {{ user.getFullNameOrUsername }}">
<dl>
{% if user.getFullName  %}
    <dt>Full Name</dt>
    <dd>{{ user.getFullName }}</dd>
{% endif %}

<dt>Email</dt>
<dd>{{ user.email }}</dd>
</dl>
{% endblock %}

这个页面的位置是:

http://localhost/authentication/public/u/martin 但在文件目录中为 C:\xampp2\htdocs\authentication\app\views\user\profile.php

一旦用户点击“发送好友请求”按钮,就会运行以下js:

$( document ).ready(function() {


  $("#sfr").click(function( event ) {
  event.preventDefault();
  $.ajax({
  method: "POST",
  url: "http://localhost/authentication/app/routes/account/friend.php",
  data: { fr: "sfr"}
})
  .done(function( msg ) {
    console.log(msg);
    alert( msg );
  });

});     

});

我创建了一条名为friend的路线,它看起来像这样:

<?php

$app->post('/profile', $authenticated(), function() use ($app) {

    $app = \Slim\Slim::getInstance();

    $request = $app->request();

    $fr = $request->post('fr');

    echo "Friend Request sent";


})->name('account.friend.post');

我收到以下错误消息:

<br />
<b>Notice</b>:  Undefined variable: app in <b>C:\xampp2\htdocs\authentication\app\routes\account\friend.php</b> on line    <b>3</b><br />


<b>Fatal error</b>:  Call to a member function post() on null in     <b>C:\xampp2\htdocs\authentication\app\routes\account\friend.php</b> on line   <b>3</b><br />

我认为问题出在 ajax 调用中的 url 中。我希望我可以将它路由到路由的名称而不是它的位置。

任何帮助将不胜感激。谢谢!

【问题讨论】:

  • 您必须使用require 'path_to_slim/Slim.php'; \Slim\Slim::registerAutoloader();包含苗条框架

标签: php jquery ajax slim


【解决方案1】:

我认为问题在于您正在调用friend.php 文件而不是调用路由。在 AJAX 代码中的 URL 上,调用路由: $( 文档 ).ready(function() {

$("#sfr").click(function( event ) {
   event.preventDefault();
   $.ajax({
      method: "POST",
      url: "http://localhost/authentication/app/routes/account/profile",
      data: { fr: "sfr"}
   })
   .done(function( msg ) {
      console.log(msg);
      alert( msg );
   });
});

确保您已在 Slim 应用实例化中注册了 POST 路由 /profile

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-08
    • 1970-01-01
    • 1970-01-01
    • 2017-12-12
    • 2014-12-30
    • 2011-10-12
    • 2017-07-23
    • 2013-06-12
    相关资源
    最近更新 更多