如何处理rest native中的restful web服务响应?

时间:2018-02-22 11:28:14

标签: rest react-native

这就是我调用网络服务的方式。

func attributedString(parentString:String, arrayOfStringToProcess:[String], color:UIColor) -> NSAttributedString
{
    let parentAttributedString = NSMutableAttributedString(string:parentString, attributes:nil)
    let parentStringWords = parentAttributedString.string.components(separatedBy: " ")
    if parentStringWords.count != 0
    {
        let wordSearchArray = arrayOfStringToProcess.filter { inputArrayIndex in
            parentStringWords.contains(where: { $0 == inputArrayIndex }
            )}
        for eachWord in wordSearchArray
        {
            parentString.enumerateSubstrings(in: parentString.startIndex..<parentString.endIndex, options: .byWords)
            {
                (substring, substringRange, _, _) in
                if substring == eachWord
                {
                    parentAttributedString.addAttribute(.font, value: UIFont.boldSystemFont(ofSize: 15), range: NSRange(substringRange, in: parentString))
                    parentAttributedString.addAttribute(.foregroundColor, value: color, range: NSRange(substringRange, in: parentString))
                }
            }
        }
    }
    return parentAttributedString
}

当我从邮递员那里打电话时,我得到了这样的回复:

fetch('http://**someurl**', {  
  method: 'POST',
  headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    username : "admin",
    password: "admin123",
  })
})
  

有谁能告诉我如何处理回复?

1 个答案:

答案 0 :(得分:2)

希望这有帮助。

fetch(url, {
    method: 'POST',
    headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json',
    },
    body: JSON.stringify({
    username : "admin",
    password: "admin123",
  })
}).then((response) => response.json()).then((responseJson) => {
        // you'll get the response in responseJson
    })
    .catch((error) => {
        //you will get error here.
    });