JSON帖子似乎没有发布,我得到所有对象而不是将我的对象发布到列表中

时间:2016-06-19 18:17:30

标签: c# json post httpwebrequest

我试图使用HttpWebRequest类发布数据。我用post方法调用的函数应该将我的数据添加到列表中并返回刚刚添加的数据。但是,我没有发布我尝试发送的数据,而是将列表中的所有数据都返回,这就是等效的get方法应该做的事情。这是代码:

import Foundation

import UIKit

class Case1Q3ViewController: UIViewController, UITextFieldDelegate {

    @IBOutlet weak var nextButton: UIButton!
    @IBOutlet var questionLabel: UILabel!

    @IBOutlet var correctAnswerLabel: UILabel!

    @IBOutlet var inputTextField: UITextField!

    var enteredAnswer: String?

    var correctAnswer = "Well demarcated"

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(InputViewController1.keyboardWillShow(_:)), name: UIKeyboardWillShowNotification, object: nil)
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(InputViewController1.keyboardWillHide), name: UIKeyboardWillHideNotification, object: nil)

        inputTextField.delegate = self

        titlesForLabels()
        nextButton.enabled = false
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func titlesForLabels() {
        questionLabel.text = "Describe the borders"
        correctAnswerLabel.text = correctAnswer
        correctAnswerLabel.hidden = true

        inputTextField.text = nil
        inputTextField.enabled = true
    }

    func keyboardWillShow(notification: NSNotification) {
        let userInfo = notification.userInfo!

        let keyboardFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()

        UIView.animateWithDuration(0.1, animations: { () -> Void in
            self.view.frame.origin.y = -keyboardFrame.size.height
        })
    }

    func keyboardWillHide() {
        UIView.animateWithDuration(0.1, animations: { () -> Void in
            self.view.frame.origin.y = 0
        })
    }

    func textFieldShouldReturn(textField: UITextField) -> Bool {
        textField.resignFirstResponder()

        enteredAnswer = textField.text

        checkForCorrectAnswer()

        return true
    }

    func checkForCorrectAnswer() {
        if enteredAnswer!.lowercaseString == correctAnswer.lowercaseString {
            print("Correct")
            correctAnswerLabel.textColor = UIColor.greenColor()
            correctAnswerLabel.text = "Correct!"
            nextButton.enabled = true

        } else {
            print("Wrong Answer")
            correctAnswerLabel.textColor = UIColor.redColor()
            correctAnswerLabel.text = "Incorrect! Please try again"
            nextButton.enabled = false

        }
        correctAnswerLabel.hidden = false
    }
    override func viewWillAppear(animated: Bool) {
        navigationItem.title = "Case 1 - Q3"
    }
}

所以我开始认为我正在调用的API认为我在实际尝试发布时会得到。任何帮助将不胜感激,提前谢谢。

编辑:对于记录,如果最后尝试过带有反斜杠的URL,没有它,则不会改变任何内容。

1 个答案:

答案 0 :(得分:0)

您正在使用POST请求调用您的方法,如下所示。您确定您的API方法是POST方法。确保它是,在这种情况下,它应该有[System.Web.Http.HttpPost]属性。

httpWebRequest.Method = "POST";