我正在使用此代码将 JSON 对象发布到 URL groovy:
def http = new HTTPBuilder( 'myURL' )
// perform a POST request, expecting JSON response data
http.request( POST, JSON ) {
uri.path = myPath
uri.query = [ service_key:'1.0', event_type: 'trigger' ]
headers.'Content-Type' = 'application/json'
// response handler for a success response code:
response.success = { resp, json ->
println resp.status
// parse the JSON response object:
json.responseData.results.each {
ret = json.getText()
println 'Response data: -----'
println ret
println '--------------------'
}
}
// handler for any failure status code:
response.failure = { resp ->
println "Unexpected error: ${resp.status} : ${resp.statusLine.reasonPhrase}"
}
}
Ajax Code that works:(EDITED)
$.ajax({ url:'https://events.pagerduty.com/generic/2010-04-15/create_event.json',
type: 'POST',
contentType: 'application/json',
data: JSON.stringify({
service_key: "1379ca7018e94343bf5fa5add9fa42eb",
incident_key: "srv01/HTTP",
event_type: "trigger",
description: "TEst Test"
}),
dataType:'json'
});
alert('Message Sent');
每次我收到意外错误:400:错误请求,如果我用 $.ajax() 做同样的事情,它就可以工作。我在响应中得到 HTTP:200 OK。这里出了什么问题?
谢谢你。