是否可以在Javascript(Ajax - > PHP)中POST一个类方法?

时间:2015-08-06 11:48:30

标签: javascript php ajax

我正在使用Ajax处理带有进度条的文件上传,目前我发布到上传脚本:

object(GuzzleHttp\Psr7\Response)#169 (6) {
  ["reasonPhrase":"GuzzleHttp\Psr7\Response":private]=>
  string(2) "OK"
  ["statusCode":"GuzzleHttp\Psr7\Response":private]=>
  int(200)
  ["headers":"GuzzleHttp\Psr7\Response":private]=>
  array(12) {
    ["access-control-allow-origin"]=>
    array(1) {
      [0]=>
      string(1) "*"
    }
    ["content-type"]=>
    array(1) {
      [0]=>
      string(31) "application/json; charset=UTF-8"
    }
    ["x-fb-trace-id"]=>
    array(1) {
      [0]=>
      string(11) "XXXXXXX"
    }
    ["x-fb-rev"]=>
    array(1) {
      [0]=>
      string(7) "XXXXXXX"
    }
    ["pragma"]=>
    array(1) {
      [0]=>
      string(8) "no-cache"
    }
    ["cache-control"]=>
    array(1) {
      [0]=>
      string(44) "private, no-cache, no-store, must-revalidate"
    }
    ["facebook-api-version"]=>
    array(1) {
      [0]=>
      string(4) "v2.3"
    }
    ["expires"]=>
    array(1) {
      [0]=>
      string(29) "Sat, 01 Jan 2000 00:00:00 GMT"
    }
    ["x-fb-debug"]=>
    array(1) {
      [0]=>
      string(88) "XXXXXXXXX=="
    }
    ["date"]=>
    array(1) {
      [0]=>
      string(29) "Fri, 07 Aug 2015 11:32:55 GMT"
    }
    ["connection"]=>
    array(1) {
      [0]=>
      string(10) "keep-alive"
    }
    ["content-length"]=>
    array(1) {
      [0]=>
      string(3) "279"
    }
  }
  ["headerLines":"GuzzleHttp\Psr7\Response":private]=>
  array(12) {
    ["Access-Control-Allow-Origin"]=>
    array(1) {
      [0]=>
      string(1) "*"
    }
    ["Content-Type"]=>
    array(1) {
      [0]=>
      string(31) "application/json; charset=UTF-8"
    }
    ["X-FB-Trace-ID"]=>
    array(1) {
      [0]=>
      string(11) "HkN84RYQnOf"
    }
    ["X-FB-Rev"]=>
    array(1) {
      [0]=>
      string(7) "1871945"
    }
    ["Pragma"]=>
    array(1) {
      [0]=>
      string(8) "no-cache"
    }
    ["Cache-Control"]=>
    array(1) {
      [0]=>
      string(44) "private, no-cache, no-store, must-revalidate"
    }
    ["Facebook-API-Version"]=>
    array(1) {
      [0]=>
      string(4) "v2.3"
    }
    ["Expires"]=>
    array(1) {
      [0]=>
      string(29) "Sat, 01 Jan 2000 00:00:00 GMT"
    }
    ["X-FB-Debug"]=>
    array(1) {
      [0]=>
      string(88) "XXXXXXXXXX=="
    }
    ["Date"]=>
    array(1) {
      [0]=>
      string(29) "Fri, 07 Aug 2015 11:32:55 GMT"
    }
    ["Connection"]=>
    array(1) {
      [0]=>
      string(10) "keep-alive"
    }
    ["Content-Length"]=>
    array(1) {
      [0]=>
      string(3) "279"
    }
  }
  ["protocol":"GuzzleHttp\Psr7\Response":private]=>
  string(3) "1.1"
  ["stream":"GuzzleHttp\Psr7\Response":private]=>
  object(GuzzleHttp\Psr7\Stream)#161 (7) {
    ["stream":"GuzzleHttp\Psr7\Stream":private]=>
    resource(224) of type (stream)
    ["size":"GuzzleHttp\Psr7\Stream":private]=>
    NULL
    ["seekable":"GuzzleHttp\Psr7\Stream":private]=>
    bool(true)
    ["readable":"GuzzleHttp\Psr7\Stream":private]=>
    bool(true)
    ["writable":"GuzzleHttp\Psr7\Stream":private]=>
    bool(true)
    ["uri":"GuzzleHttp\Psr7\Stream":private]=>
    string(10) "php://temp"
    ["customMetadata":"GuzzleHttp\Psr7\Stream":private]=>
    array(0) {
    }
  }
}

但是,如果可能的话,我想将我的上传内容转移到一个类方法中(让我们说这是一个名为FileHandler的类,我希望能够发布它的upload()函数

是否可以将其发布到班级的上传方式?

显然我希望能够保留加载栏的功能,如果有人需要查看Javascript的源代码,这里有一个链接到我实际上复制的地方 - 面包代码和做了一些更改(upload_form.html):

JavaScript File Upload

2 个答案:

答案 0 :(得分:1)

据我所知,你不能。您可以做的是为您的类提供一个处理程序文件,例如,假设我们有这个PHP类,

<?php
class Car {     
    function getCarType() {
        return "Super Car";
    }
}
?>

然后在你的处理程序文件中,

<?php
require_once 'Car.php';

if(isset($_POST['getCarType'])) {
     $car = new Car();
     $result = $car->getCarType();
}

echo $result;
?>

您将AJAX请求发布到处理程序,您可以为每个请求创建特定的处理程序,或者您可以拥有一个通用的AJAX处理程序,但是该文件可能会变得非常大且难以维护。

答案 1 :(得分:0)

这不是关于javascript的问题,而是关于«如何在PHP中正确路由请求?» 试着看看Symfony的路由器: https://symfony.com/doc/current/book/routing.html