我们假设这个URL ...
http://www.example.com/page.php?id=10
Run Code Online (Sandbox Code Playgroud)
(这里需要在POST请求中发送id)
我想发送id = 10到服务器page.php,它在POST方法中接受它.
我怎样才能从Java中做到这一点?
我试过这个:
URL aaa = new URL("http://www.example.com/page.php");
URLConnection ccc = aaa.openConnection();
Run Code Online (Sandbox Code Playgroud)
但我仍然无法弄清楚如何通过POST发送它
这是我正在使用的代码:
// create a request
HttpWebRequest request = (HttpWebRequest)
WebRequest.Create(url); request.KeepAlive = false;
request.ProtocolVersion = HttpVersion.Version10;
request.Method = "POST";
// turn our request string into a byte stream
byte[] postBytes = Encoding.UTF8.GetBytes(json);
// this is important - make sure you specify type this way
request.ContentType = "application/json; charset=UTF-8";
request.Accept = "application/json";
request.ContentLength = postBytes.Length;
request.CookieContainer = Cookies;
request.UserAgent = currentUserAgent;
Stream requestStream = request.GetRequestStream();
// now send it
requestStream.Write(postBytes, 0, postBytes.Length);
requestStream.Close();
// grab te response and print it out …Run Code Online (Sandbox Code Playgroud) 我在我的MooTools脚本中运行一个AJAX调用,这在Firefox中工作正常但在Chrome中我收到Uncaught SyntaxError: Unexpected token :错误,我无法确定原因.注释掉代码来确定坏代码的位置什么都没有产生,我想这可能是返回JSON的问题.检查控制台我看到返回的JSON是这样的:
{"votes":47,"totalvotes":90}
Run Code Online (Sandbox Code Playgroud)
我没有看到任何问题,为什么会出现这种错误?
vote.each(function(e){
e.set('send', {
onRequest : function(){
spinner.show();
},
onComplete : function(){
spinner.hide();
},
onSuccess : function(resp){
var j = JSON.decode(resp);
if (!j) return false;
var restaurant = e.getParent('.restaurant');
restaurant.getElements('.votes')[0].set('html', j.votes + " vote(s)");
$$('#restaurants .restaurant').pop().set('html', "Total Votes: " + j.totalvotes);
buildRestaurantGraphs();
}
});
e.addEvent('submit', function(e){
e.stop();
this.send();
});
});
Run Code Online (Sandbox Code Playgroud) POST请求中的content-type和datatype是什么?假设我有这个:
$.ajax({
type : "POST",
url : /v1/user,
datatype : "application/json",
contentType: "text/plain",
success : function() {
},
error : function(error) {
},
Run Code Online (Sandbox Code Playgroud)
是contentType我们送的吗?那么我们在上面的例子中发送的是JSON,我们收到的是纯文本?我真的不明白.
如何从Java servlet返回JSON对象.
以前在使用servlet执行AJAX时,我返回了一个字符串.是否有需要使用的JSON对象类型,或者只是返回一个看起来像JSON对象的String,例如
String objectToReturn = "{ key1: 'value1', key2: 'value2' }";
Run Code Online (Sandbox Code Playgroud) 我使用jQuery.ajax()解析从我的服务器返回的一些JSON数据时遇到困难
要执行我正在使用的AJAX:
$.ajax({
url: myUrl,
cache: false,
dataType: "json",
success: function(data){
...
},
error: function(e, xhr){
...
}
});
Run Code Online (Sandbox Code Playgroud)
如果我返回一个项目数组,那么它工作正常:
[ { title: "One", key: "1" }, { title: "Two", key: "2" } ]
Run Code Online (Sandbox Code Playgroud)
调用success函数并接收正确的对象.
但是,当我尝试返回单个对象时:
{ title: "One", key: "1" }
Run Code Online (Sandbox Code Playgroud)
调用错误函数,xhr包含'parsererror'.我已经尝试将JSON包装在服务器上的括号中,然后再将其发送到网络中,但它没有任何区别.然而,如果我将内容粘贴到Javascript中的字符串中然后使用eval()函数,它会完美地评估它.
我有什么想法我做错了吗?
安东尼
问题是:我有一个在PHP服务器上运行的Web应用程序.我想为它构建一个REST api.
我做了一些研究,我发现REST api使用HTTP方法(GET,POST ...)为某些带有身份验证密钥的URI(不一定),并将信息作为HTTP响应呈现回来,信息为XML或JSON (我宁愿JSON).
我的问题是:
我的REST API返回JSON.
我目前正在返回text/plain作为MIME类型,但感觉很有趣.我应该回来application/x-javascript还是其他类型?
第二个问题是关于错误条件的HTTP状态代码.如果我的REST API返回错误状态,我将返回JSON
{ result: "fail", errorcode: 1024, errormesg: "That sucked. Try again!" }
Run Code Online (Sandbox Code Playgroud)
HTTP状态代码应该保留在200 OK?