1
我試圖與JavaScriptCore.framework
一起玩,我試圖做一個http post請求,但我沒有得到任何答覆。在iOS的JavaScriptCore中獲取http post響應
我的代碼:
目標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瀏覽器什麼。
也許我做錯了什麼?