我有一个.aspx
文件,里面有一些网络方法。我想从 iOS (iPhone) 应用程序中使用这些方法。web方法代码如下:
[WebMethod]
public static string Login(string username, string password)
{
return username + ":" + password;
}
为了测试这个网络方法,我使用了:
$.ajax({
type: "POST",
url: "DataProvider.aspx/Login",
data: JSON.stringify({ username: "myUserName", password: "myPassword" }),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) { alert(result.d); },
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
}
});
所以一切都检查完了,我"myUserName:myPassword"
回来了,现在我想从 xcode 4.5.2 内部做同样的事情,所以我创建了一个新的 iPhone 应用程序,并在 ViewController 中放置一个label
(lbResult) 和一个button
(btDoLogin) 并分配出口和动作.
请注意,我对异步或委托不感兴趣,我只想返回数据并能够解析它(JSON)。
很抱歉对细节如此具体,但我看到了很多类似的问题,但没有一个答案对我有用。对于我所读到的内容,我的理解是可以使用 NSURLConnection 来解决这个问题。例如这个问题Passing parameters to a JSON web service in Objective C和我需要的非常相似,但是我得到的是整个页面!(意味着它正在下载整个页面而不是调用 web 方法)另外,我需要传递 2 个参数并且问题只使用 1(我不明白如何在连接的字符串中将它们分开)。
现在,我究竟需要在 IBAction 内部做什么才能连接到这个特定的 webmethod 并获取返回值?
- (IBAction)btDoLogin:(id)sender {
// magic code goes here!
}
谢谢。-