1
我知道有關於堆棧溢出的類似問題,但沒有解決我的問題。 我做一個簡單的NSMutableRequest到URL和發佈一個簡單的字符串:NSMutableURLRequest POST 405錯誤獲取
let locationString = "myString"
let request = NSMutableURLRequest(URL: NSURL(string: "http://exampleurl.com")!)
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.HTTPMethod = "POST"
request.HTTPBody = locationString.dataUsingEncoding(NSUTF8StringEncoding)
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
data, response, error in
if error != nil {
print("error=\(error)")
return
}
print("response = \(response)")
let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
print("responseString = \(responseString)")
}
task.resume()
,但我得到一個405錯誤:「方法來獲取不允許」
不知道我在做什麼錯
感謝
https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSJSONSerialization_Class/#//apple_ref/occ/clm/NSJSONSerialization/dataWithJSONObject:options:error –
謝謝@LeoDabus,但我不能使用NSJSONSerialization作爲API調用需要按順序發送鍵/值。我知道這是不對的,但我無法控制這一點。 – mrcurious
你能解釋一下爲什麼我用當前的方法得到這個錯誤嗎? – mrcurious