嘿,我已经检查了网络并尝试了所有建议的解决方案
但仍然是UNdefined
const NSString * kAppHost = @“myApp.example.org”;
- (NSCachedURLResponse *)cachedResponseForRequest:(NSURLRequest *)request {
NSURL *url = [request URL];
// check the host to see if Javascript is trying to send a request to our app's "fake" host
if ([[url host] caseInsensitiveCompare:(NSString *) kAppHost] == NSOrderedSame) {
NSString *action = nil;
if ([[url pathComponents] count] > 1) { // use index 1 since index 0 is the '/'
// Theoretically, we could use the :controller/:action/:id pattern here, but for simplicity we'll just do
// /:action
action = [[url pathComponents] objectAtIndex:1];
}
NSString *query = [url query];
NSString *method = [request HTTPMethod];
NSDictionary *params = nil;
// we also want to extract any arguments passed in the request. In a GET, we can get these from the URL query
// string. In requests with entities, we can get this from the request body (assume www-form encoded for our purposes
// here, but we could also handle JSON entities)
if ([method isEqualToString:@"POST"] || [method isEqualToString:@"PUT"]) {
NSString *body = nil;
body = [[NSString alloc] initWithData:[request HTTPBody] encoding:NSUTF8StringEncoding];
params = [NSDictionary gtm_dictionaryWithHttpArgumentsString:body];
} else {
params = [NSDictionary gtm_dictionaryWithHttpArgumentsString:query];
}
// construct a NativeAction object to transport this request message to our handler in native code
//NativeAction *nativeAction;// = [[NativeAction alloc] initWithAction:action method:method params:params];
NSError *error1 = nil;
NSMutableDictionary *result = [[NSMutableDictionary alloc] init];// = [[self.delegate handleAction:nativeAction error:&error1] mutableCopy];
[result setObject:@"1234" forKey:@"guid"];
// if we got an error, assign it in the hash we'll pass back to javascript
if (error1) {
[result setObject:@{
@"code" : [NSNumber numberWithInt:error1.code],
@"message" : [error1 localizedDescription]
} forKey:@"error"];
}
// Lastly, we need to construct an NSCachedURLResponse object to return from this method. This is the response
// (headers + body) that will be passed back to our jQuery callback
NSCachedURLResponse *cachedResponse = nil;
if (result) {
NSString *jsonString = [[[SBJsonWriter alloc] init] stringWithObject:result];
NSData *jsonBody = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *headers = @{@"Access-Control-Allow-Origin" : @"*", @"Access-Control-Allow-Headers" : @"Content-Type, Origin, Accept, x-requested-with", @"" : @"GET, POST"};
NSHTTPURLResponse *urlresponse = [[NSHTTPURLResponse alloc] initWithURL:request.URL statusCode:200 HTTPVersion:@"1.1" headerFields:headers];
cachedResponse = [[NSCachedURLResponse alloc] initWithResponse:urlresponse data:jsonBody];
NSLog(@"cachedResponse %@", cachedResponse);
NSLog(@"cachedResponse data %@", [[NSString alloc] initWithData:[cachedResponse data] encoding:NSUTF8StringEncoding]);
}
return cachedResponse;
}
// if not matching our custom host, allow system to handle it
return [super cachedResponseForRequest:request];
}
在这里,我看到一切都很好,但在ajax中,我得到了未定义的
function GetDataIphone() {
$.ajax({
type: "POST",
url: "http://myApp.example.org/contacts",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
alert("getdata");
alert(data);
$('#message')[0].innerHTML = data.responseText;
},
error: function (ex) {
$('#message')[0].innerHTML ="ERROR" + ex.message;
}
});
答案 0 :(得分:0)
我尝试了类似的方法,但从未成功地使用POST。但是,有一个更好的选择:您可以使用NSURLProtocol
堆栈来拦截和处理任何请求。
WebViewProxy组件提供了可重用的易用实现。