我目前正在实现一个 RESTful Web 服务,它使用CodeIgniter和Phil Sturgeon的 REST 来讨论 XML 。我现在被困在如何从 HTTP PUT 读取 XML。这就是我所做的。
在客户端:
$(function(){
// Bind a click event to the 'ajax' object id
$("#new_user").click(function(evt){
// JavaScript needs totake over. So stop the browser from redirecting the page
evt.preventDefault();
var str = '<?xml version="1.0" encoding="UTF-8"?><xml><name>'+$("#txtname").val()+'</name><email>'+$("#txtemail").val()+'</email></xml>';
// Ajax request to get the data
$.ajax({
// URL from the link that was clicked on
url: $(this).attr("href"),
type: "put",
contentType: "application/xml",
processData: false,
data: str,
success: function(data, textStatus, jqXHR){
//alert('Successful AJAX request!');
//var items = parseXml(data);
//printXml(items);
},
// Failed to load request. This could be caused by any number of problems like server issues, bad links, etc.
error: function(jqXHR, textStatus, errorThrown){
alert('Oh no! A problem with the Ajax request!');
}
});
});
});
在服务器端:
public function users_put(){
$input = file_get_contents('php://input');
print_r($input);
}
它什么也没打印出来。上面的 JavaScript 代码和函数在 HTTP POST 中运行良好。