2016-05-03 33 views
1

我在使用POST和php REST服務器時遇到問題。 file_get_contents("php://input")正在添加額外的引號。 這是造成json_decode(file_get_contents("php://input"),true)到 失敗php file_get_contents(「php:// input」)加入引號

即 我張貼字符串化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"}

我移動後最初使用擊中錯誤在角應用角ngResource

控制器

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}, 
    }); 
    } 

})(); 
+1

證明帖子是JSON字符串 – RiggsFolly

+0

好代碼,你不應該跟單蜱''來輸出'。如果你不能修改它的輸出,那麼'preg_replace(「@(^'|'$)@」,'',file_get_contents(「php:// input」))' – DataHerder

+1

你如何餵養JSON? –

回答

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

在webstorm調試如果你正在傳入第一個變體,當你在

var jsonData = JSON.stringify(data); 

結果傳給你可能已經解決你的問題,你自己....

你應該得到類似PHP巧妙地想出並返回一個字符串...

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

0

原來我只是在問完全錯誤的問題

我的角度應用程序由於CORS而導致POST失敗。我在本地主機上運行應用程序,但查詢遠程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