Created
August 22, 2013 11:45
-
-
Save tangyangzhe/6306188 to your computer and use it in GitHub Desktop.
提交JSON格式数据到服务端
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 前端JS代码 */ | |
function addWine() { | |
console.log('addWine'); | |
$.ajax({ | |
type: 'POST', | |
contentType: 'application/json', | |
url: rootURL, | |
dataType: "json", | |
data: formToJSON(), | |
success: function(data, textStatus, jqXHR){ | |
alert('Wine created successfully'); | |
$('#btnDelete').show(); | |
$('#wineId').val(data.id); | |
}, | |
error: function(jqXHR, textStatus, errorThrown){ | |
alert('addWine error: ' + textStatus); | |
} | |
}); | |
} | |
function updateWine() { | |
console.log('updateWine'); | |
$.ajax({ | |
type: 'PUT', | |
contentType: 'application/json', | |
url: rootURL + '/' + $('#wineId').val(), | |
dataType: "json", | |
data: formToJSON(), | |
success: function(data, textStatus, jqXHR){ | |
alert('Wine updated successfully'); | |
}, | |
error: function(jqXHR, textStatus, errorThrown){ | |
alert('updateWine error: ' + textStatus); | |
} | |
}); | |
} | |
function formToJSON() { | |
return JSON.stringify({ | |
"id": $('#wineId').val(), | |
"name": $('#name').val(), | |
"grapes": $('#grapes').val(), | |
"country": $('#country').val(), | |
"region": $('#region').val(), | |
"year": $('#year').val(), | |
"picture": currentWine.picture, | |
"description": $('#description').val() | |
}); | |
} | |
/* 服务端代码 PHP */ | |
<?php | |
require 'Slim/Slim.php'; | |
$app = new Slim(); | |
$app->get('/wines', 'getWines'); | |
$app->get('/wines/:id', 'getWine'); | |
$app->get('/wines/search/:query', 'findByName'); | |
$app->post('/wines', 'addWine'); | |
$app->put('/wines/:id', 'updateWine'); | |
$app->delete('/wines/:id', 'deleteWine'); | |
$app->run(); | |
function addWine() { | |
error_log('addWine\n', 3, '/var/tmp/php.log'); | |
$request = Slim::getInstance()->request(); | |
$wine = json_decode($request->getBody()); | |
$sql = "INSERT INTO wine (name, grapes, country, region, year, description) VALUES (:name, :grapes, :country, :region, :year, :description)"; | |
try { | |
$db = getConnection(); | |
$stmt = $db->prepare($sql); | |
$stmt->bindParam("name", $wine->name); | |
$stmt->bindParam("grapes", $wine->grapes); | |
$stmt->bindParam("country", $wine->country); | |
$stmt->bindParam("region", $wine->region); | |
$stmt->bindParam("year", $wine->year); | |
$stmt->bindParam("description", $wine->description); | |
$stmt->execute(); | |
$wine->id = $db->lastInsertId(); | |
$db = null; | |
echo json_encode($wine); | |
} catch(PDOException $e) { | |
error_log($e->getMessage(), 3, '/var/tmp/php.log'); | |
echo '{"error":{"text":'. $e->getMessage() .'}}'; | |
} | |
} | |
function updateWine($id) { | |
$request = Slim::getInstance()->request(); | |
$body = $request->getBody(); | |
$wine = json_decode($body); | |
$sql = "UPDATE wine SET name=:name, grapes=:grapes, country=:country, region=:region, year=:year, description=:description WHERE id=:id"; | |
try { | |
$db = getConnection(); | |
$stmt = $db->prepare($sql); | |
$stmt->bindParam("name", $wine->name); | |
$stmt->bindParam("grapes", $wine->grapes); | |
$stmt->bindParam("country", $wine->country); | |
$stmt->bindParam("region", $wine->region); | |
$stmt->bindParam("year", $wine->year); | |
$stmt->bindParam("description", $wine->description); | |
$stmt->bindParam("id", $id); | |
$stmt->execute(); | |
$db = null; | |
echo json_encode($wine); | |
} catch(PDOException $e) { | |
echo '{"error":{"text":'. $e->getMessage() .'}}'; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment