【问题标题】:PHP Symfony API and jQuery Ajax requestPHP Symfony API 和 jQuery Ajax 请求
【发布时间】:2016-11-18 10:18:09
【问题描述】:

我在服务器端和客户端之间有问题。我在服务器端有一个使用 PHP Symfony 的 Rest API。 服务器端:

/**
* @Route("/advertisement/all", name="advertisement_get_all")
* @Method("POST")
*/
public function getAllAdvertisement()
{
  header('Content-Type: application/json');
  if ($this->get('security.authorization_checker')->isGranted('ROLE_ADMIN')) {
    $content = $this->get("request")->getContent();
    $advertisemenetService = $this->container->get("advertisementservices");
    $response = $advertisemenetService->getAllAdvertisement($content);
  } else {
    $response = new \stdClass();
    $response->error = true;
    $response->message = "Error occurred: You aren't authorized!";
  }

  return new JsonResponse($response);
}

如果我在 DHC Rest Client Chrome 扩展中尝试它 development.domain.com/index.php/api/v2/advertisement/all 内容类型:application/x-www-form-urlencoded 我得到了一个正确的 JSON 对象。如果我对application/json 尝试相同的操作,symfony 会为我说出以下错误: Failed to start the session because headers have already been sent by "" at line 0. (500 Internal Server Error) JSON response example

客户端:

我在 API 测试仪上说过,我得到了一个正确的 JSON 对象。我的客户端代码:

function sendAjaxRequest(method, url, data, contentType) {
    var response;
    $.ajax({
        method: method,
        url: url,
        data: data,
        dataType: 'json',
        contentType: contentType,
        success: function(msg) {
            response = msg;
        }
    });
    return jQuery.parseJSON(response);
}
response = sendAjaxRequest("POST", "{{ path('advertisement_get_all') }}", '', 'application/x-www-form-urlencoded');
document.getElementById("loader-container").innerHTML = response;

在这种情况下,我总是在客户端收到undefined。我尝试在响应中使用 JSON.stringify,因为它是一个 JSON 对象。

【问题讨论】:

  • 您在使用JsonResponse 时不想添加header 功能。评论行:header('Content-Type: application/json');

标签: javascript php jquery json symfony


【解决方案1】:

将 loader-container 的更新移动到成功处理程序中。

function sendAjaxRequest(method, url, data, contentType) {
    var response;
    $.ajax({
        method: method,
        url: url,
        data: data,
        dataType: 'json',
        contentType: contentType,
        success: function(msg) {
            document.getElementById("loader-container").innerHTML = JSON.parse(msg);
        }
    });
}

【讨论】:

  • 基本上我做了同样的解决方案,只是使用了 XMLHttpRequest。这也是一个很好的解决方案,但对我来说有点麻烦,这就是我将其更改为旧解决方案的原因。
【解决方案2】:

我放弃了 jQuery Ajax,因为它太难解决这个问题,然后回到 XMLHttpRequest 并使用我在 AJAX 获得响应时调用的回调函数。

function sendAjaxRequest(method, url, data, contentType, callback)
{
    var response
    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            response = xhttp.responseText;
            callback(JSON.parse(response));
        }
    };
    xhttp.open(method, url, true);
    xhttp.setRequestHeader("Content-Type", contentType);
    xhttp.send(data);

    return response;
}

出现的主要问题:响应处理函数总是在 Ajax 响应到达之前运行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-22
    • 1970-01-01
    • 2019-09-02
    • 2014-09-13
    • 2015-09-07
    • 2013-04-12
    相关资源
    最近更新 更多