php file_get_contents(“php:// input”)添加引号

时间:2016-05-03 22:57:29

标签: php angularjs json file-get-contents ngresource

我在使用带有php REST服务器的POST时遇到问题。 file_get_contents("php://input")正在添加其他引号。 这导致json_decode(file_get_contents("php://input"),true) 失败

即。 我发布了字符串化的JSON

'{"someValue":0,"someOtherValue":1}'

PHP:

var_dump(file_get_contents("php://input"))

返回

string(173) "'{  "someValue" : 0,   "someOtherValue" : "1"}'"

我的PHP版本是5.3.10

要发布json,我目前正在使用webstorm REST客户端工具

Headers: 
    Accept: */*
Request Body:
    Text: '{  "someValue" : 0,   "someOtherValue" : "1"}'

我尝试从webstorm中的字符串中删除外部引号,它将工作于I.E. { "someValue" : 0, "someOtherValue" : "1"}

在最初使用angular ngResource

在角度应用程序中遇到错误后,我开始调试webstorm

控制器

angular
    .module('app.bookings')
    .controller('BookAPuntController', BookAPuntController);
BookAPuntController.$inject(BookingServices);
function BookAPuntController(BookingServices) {
    var data = {
      someValue:0,
      someOtherValue:1
    };
    BookingServices.save(JSON.stringify(data));
};

booking.dataservice.js

(function () {
  'use strict';

  angular
    .module('app.data')
    .factory('BookingServices', BookingServices);

  BookingServices.$inject = ['$resource'];

  /* @ngInject */
  function BookingServices($resource) {
    return $resource('rest/booking/:Id/:from/:to', null, {
      'get': {method: 'GET', isArray: true},
    });
  }

})();

2 个答案:

答案 0 :(得分:3)

'{"someValue":0,"someOtherValue":1}'; // IS A STRING...
 {"someValue":0,"someOtherValue":1};  // IS NOT A STRING...

如果你传入第一个变种;你应该找回像PHP这样聪明地想出来的字符串并返回...

string(173) "'{  "someValue" : 0,   "someOtherValue" : "1"}'"

传入

的结果时
var jsonData = JSON.stringify(data);

你可能已经解决了问题,你自己......

答案 1 :(得分:0)

原来我问的是完全错误的问题

由于CORS,我的角度应用程序失败了它的POST。我在localhost上运行应用程序,但查询远程REST php服务器。当我运行应用程序时,由于CORS,正在发送方法OPTIONS请求。服务器不知道如何回应,所以一切都失败了。

webstorm中的调试是人为地引入原始问题中看到的错误。

Why am I getting an OPTIONS request instead of a GET request?

https://serverfault.com/questions/231766/returning-200-ok-in-apache-on-http-options-requests