在JavaScriptCore for iOS中获取http post响应
我正在尝试使用在JavaScriptCore for iOS中获取http post响应,javascript,ios,objective-c,Javascript,Ios,Objective C,我正在尝试使用JavaScriptCore.framework并尝试发出一个HTTPPOST请求,但根本没有得到响应 我的代码: 目标C: #define UTILITY_PATH ([[NSBundle mainBundle] pathForResource:@"GeneralUtility" ofType:@"js"]) #define WEB_SERVICE_PATH ([[NSBundle mainBundle] pathForResource:@"WebServic
JavaScriptCore.framework
并尝试发出一个HTTPPOST请求,但根本没有得到响应
我的代码:
目标C:
#define UTILITY_PATH ([[NSBundle mainBundle] pathForResource:@"GeneralUtility" ofType:@"js"])
#define WEB_SERVICE_PATH ([[NSBundle mainBundle] pathForResource:@"WebService" ofType:@"js"])
- (void)testJavaScript
{
NSError *error = nil;
JSContext *context = [[JSContext alloc] init];
NSString *scriptString = [NSString stringWithContentsOfFile:WEB_SERVICE_PATH encoding:NSUTF8StringEncoding error:nil];
if (error) {
NSLog(@"%s, error: %@", __PRETTY_FUNCTION__, error.localizedDescription);
}
[context evaluateScript:scriptString];
context[@"print"] = ^(NSString *text) {
NSLog(@"text: %@", text);
};
JSValue *function = context[@"getGoogleLocationResponse"];
[function callWithArguments:@[]];
}
和JavaScript:
function getGoogleLocationResponse()
{
var lat = "31.1", lon = "34.4";
var url = "http://maps.googleapis.com/maps/api/geocode/json?latlng="+lat+","+lon"+&sensor=false";
var request = new XMLHttpRequest();
request.open("GET", url, false);
request.setRequestHeader("Content-Type", "text/plain");
request.send(null);
print("response text: "+request.responseText);
}
当我读到XLHttpRequest
时,我没有看到任何关于不支持safari浏览器的内容
也许我做错了什么?[[JSContext alloc]init]创建了一个独立的webkit免费JavaScript上下文实例。这意味着默认情况下只有JavaScript虚拟机核心可用:没有DOM、没有setInterval、没有XMLHttpRequest等等 如果要使用XMLHttpRequest,必须创建自定义绑定或从UIWebView获取JSContext实例