1
POST 요청을 고소하는 데이터를 게시하려고하는데 데이터를 게시 할 때마다 잘못된 요청 오류가 발생합니다. 나는 그것의 나를 위해 잘 작동하고, 목표 C에 대해 같은 논리를 사용하고신속한 JSON POST 구문 분석 문제 3
<NSHTTPURLResponse: 0x608000231e40> { URL: "REQUEST_URL" } { status code: 400, headers {
"Cache-Control" = "no-cache";
"Content-Length" = 46;
"Content-Type" = "application/json; charset=utf-8";
Date = "Wed, 30 Nov 2016 10:38:24 GMT";
Expires = "-1";
Pragma = "no-cache";
Server = "Microsoft-IIS/8.5";
"X-AspNet-Version" = "4.0.30319";
"X-Powered-By" = "ASP.NET";
"X-Powered-By-Plesk" = PleskWin;
} }
: 어떤 문제가 알려 존재 주시기 바랍니다 경우
func callDataCall()
{
let requestURL: NSURL = NSURL(string: "REQUEST_URL")!
//convert MID dict to jsondata
let NewJSONData = try! JSONSerialization.data(withJSONObject: ["Disease_request":["Mid":self.MedicationID]], options: [])
// Convert jsondata to string
let NewJSONDataString = NSString(data:NewJSONData, encoding:String.Encoding.utf8.rawValue) as! String
print("Created Dictionary is : \(NewJSONDataString)")
let urlRequest = NSMutableURLRequest(url: requestURL as URL)
// set up the session
urlRequest.httpMethod = "POST"
urlRequest.httpBody = NewJSONDataString.data(using: String.Encoding.utf8)
let task = URLSession.shared.dataTask(with: urlRequest as URLRequest)
{
data, response, error in
let httpResponse = response as! HTTPURLResponse
let statusCode = httpResponse.statusCode
print("status code is :\(httpResponse)")
if (statusCode == 200 || statusCode == 201)
{
do
{
let json = try JSONSerialization.jsonObject(with: data!, options: []) as? [String: AnyObject]
if let content = json?["Disease"] as? [[String: AnyObject]]
{
for i in 0..<content.count
{
self.medicationContent.append(content[i] as AnyObject)
}
DispatchQueue.main.async(execute: {
self.tableview_Medication.reloadData()
})
}
}
catch
{
print("Error with Json: \(error)")
}
self.stopIndicator()
}
}
task.resume()
}
응답이며,이 코드를 확인하십시오 .
'하자 reqJSONString = "\ ("{\ "Disease_request 같은 코드 뒤에 한 번 더 라인을 추가 할 필요가 \ ":") \ (JSONDataString) \ ("}") "'JSONString을 JSON에 임베드해야합니까? 또한 머리글에 길이를 설정해야 할 수도 있습니다. 실제로 콘텐츠를 게시 – Larme
하는 등 같아야합니다 { "Disease_request": { "중간": "37" }} – Piyush
이 ('URL','URLRequest','String'을 스위프트 기본 유형을 사용하도록 코드를 리팩토링하세요 ,'[String : Any]') 그리고 표준 JSON 사전은 Swift 3에서'[String : Any]'이다. 그리고'PrettyPrinted'는 잊어 버린다. 서버는 전혀 신경 쓰지 않습니다. 있는 NSDictionary = [ "중간": self.MedicationID] reqJSONString =하자 [ "Disease_request을":(MedicationIDDict)] NewJSONData는 = 해보자 – vadian