2016-09-07 101 views
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錯誤:「方法來獲取不允許」

不知道我在做什麼錯

感謝

+0

https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSJSONSerialization_Class/#//apple_ref/occ/clm/NSJSONSerialization/dataWithJSONObject:options:error –

+0

謝謝@LeoDabus,但我不能使用NSJSONSerialization作爲API調用需要按順序發送鍵/值。我知道這是不對的,但我無法控制這一點。 – mrcurious

+0

你能解釋一下爲什麼我用當前的方法得到這個錯誤嗎? – mrcurious

回答

0

我找到了解決方法,暫時至少要等到我能得到改變,接受一個JSON對象,無論爲了在服務器端代碼。

我構建了JSON作爲一個正確的順序的字符串,使用上面的方法,它的工作原理。