我已经使用laravel为GET方法成功创建了REST。我的laravel无法读取JSON数据,但如果我使用表单数据,则laravel可以正常接收/读取JSON。这是我的控制者:
const chai = require('chai')
const expect = chai.expect
chai.use(require('chai-things'))
chai.use(require('chai-like'))
describe('the chai setup', function() {
it('should work', function(done) {
const i = [{
name: 'Name A',
age: 1
}, {
name: 'Name B',
age: 2
}]
// Works
expect(i).to.include.something.that.deep.equals({
name: 'Name B',
age: 2
})
// Works
expect(i[1]).to.be.like({
name: 'Name B'
})
// Doesn't work
expect(i).to.include.something.that.like({
name: 'Name B'
})
done()
})
})
我还在我的控制器上添加了public function store(){
$tagihan=new tagihan;
$tagihan->no_rek_pelanggan=Request::input('no_rek_pelanggan');
$tagihan->keadaan_meteran=Request::input('keadaan_meteran');
$tagihan->angka_meteran=Request::input('angka_meteran');
$success=$tagihan->save();
if(!$success)
{
return Response::json("error saving",500);
}
return Response::json("success",201);
}`
。
这是我使用form-data
时的结果这是我使用raw(JSON)时的结果
{{3}}
我的laravel无法读取/接收JSON数据。
JSON我试图提交:
use Request;
[{
"id":20,
"no_rek_pelanggan":11117,
"keadaan_meteran":0,
"angka_meteran":600,
"status":0
}]
" tagihan"是我的mysql表名。
答案 0 :(得分:0)
public function store(Request $request){
$tagihan=new tagihan;
$tagihan->no_rek_pelanggan=$request->no_rek_pelanggan;
$tagihan->keadaan_meteran=$request->keadaan_meteran;
$tagihan->angka_meteran=$request->angka_meteran;
$success=$tagihan->save();
if(!$success)
{
return Response::json("error saving",500);
}
return Response::json("success",201);
}
试试这个
答案 1 :(得分:0)
你尝试过这样的事吗?您的请求数据始终采用JSON格式吗?
public function store(Request $requestData){
$request = json_encode($requestData);
//check here if all data is now good
dd($request);
$tagihan=new tagihan;
$tagihan->no_rek_pelanggan=$request->no_rek_pelanggan;
$tagihan->keadaan_meteran=$request->keadaan_meteran;
$tagihan->angka_meteran=$request->angka_meteran;
$success=$tagihan->save();
if(!$success)
{
return Response::json("error saving",500);
}
return Response::json("success",201);
}