douwaif22244
2014-08-08 14:07
浏览 44

从AngularJS发布到slim框架

Hi im making a form that send a search request to my slim API

the form works, data gets posted but in slim i get the data as a string:

form post angular $scope.submitForm = function() { console.log("--> Submitting form");

                $http({

                    url: "http://localhost/c2dapi/search",
                    data: $scope.searchForm,
                    method: 'POST',
                    headers : {'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8'}

                }).success(function(data){

                    console.log("OK", data)

                }).error(function(err){"ERR", console.log(err)})

};

//SLIM API
$app->post('/search', function () use ($app){
  $request = $app->request();
  $body = $request->getBody();
var_dump($body);
});

now this is my first angular + rest aproach so maybe im wrong but when i var dump the body in slim i get OK string(18) "{"zipCode":"3434"}"

I know there are ways to break up this string in php to get the value, but is this the right way or can i get the data also as a php array in slim ?

图片转代码服务由CSDN问答提供 功能建议

我正在制作一个向我的瘦API发送搜索请求的表单

表单工作,数据发布但是很简单我将数据作为字符串获取:

表格post angular $ scope.submitForm = function(){ console.log( “ - >提交表单”);

  $ http({
 
 url:“http:// localhost / c2dapi / search”,
 data:  $ scope.searchForm,
方法:'POST',
标题:{'Content-Type':'application / x-www-form-urlencoded; charset = UTF-8'} 
 
})。成功 (function(data){
 
 console.log(“OK”,data)
 
})。error(function(err){“ERR”,console.log(err)})
   
 
 

};

  // SLIM API 
 $ app-> post('/ search',function() 使用($ app){
 $ request = $ app-> request(); 
 $ body = $ request-> getBody(); 
var_dump($ body); 
}); 
   
 
  这是我的第一个角度+休息方式,所以也许我错了,但是当我把身体转移到苗条时我得到OK字符串(18)“{”zipCode“:”3434“}” 
 
 

我知道有一些方法可以在php中分解这个字符串来获取值,但这是正确的方法还是我可以将数据作为一个瘦弱的php数组获得?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongzhijing8202 2014-08-08 14:13
    已采纳

    You could try to decode the json string once you receive the request. Using the json_decode function

    打赏 评论

相关推荐 更多相似问题