gpt4 book ai didi

php - 预期的内容类型 text/json 得到 text/html

转载 作者:技术小花猫 更新时间:2023-10-29 11:06:00 25 4
gpt4 key购买 nike

我正在开发一个网络服务,并且正在编写一个注册/登录 API。当我尝试在 iOS 模拟器中注册时,我得到“期望的内容类型文本/json 得到文本/html”。我现在已经花了数小时尝试调试并找出问题所在,但我不明白为什么我没有获取 JSON 数据。

API.h

#import "AFHTTPClient.h"
#import "AFNetworking.h"

typedef void (^JSONResponseBlock)(NSDictionary* json);

@interface API : AFHTTPClient

@property (strong, nonatomic) NSDictionary *user;

+ (API *)sharedInstance;

//check whether there's an authorized user
-(BOOL)isAuthorized;

//send an API command to the server
-(void)commandWithParams:(NSMutableDictionary*)params onCompletion:(JSONResponseBlock)completionBlock;

@end

API.m

#import "API.h"

#define kAPIHost @"http://localhost"
#define kAPIPath @"/api/"

@implementation API

@synthesize user;

+(API*)sharedInstance
{
static API *sharedInstance = nil;
static dispatch_once_t oncePredicate;
dispatch_once(&oncePredicate, ^{
sharedInstance = [[self alloc] initWithBaseURL:[NSURL URLWithString:kAPIHost]];
});

return sharedInstance;
}

-(API*)init
{
//call super init
self = [super init];

if (self != nil) {
//initialize the object
user = nil;

[self registerHTTPOperationClass:[AFJSONRequestOperation class]];

// Accept HTTP Header; see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1
[self setDefaultHeader:@"Accept" value:@"application/json"];
}

return self;
}

-(BOOL)isAuthorized
{
return [[user objectForKey:@"user_id"] intValue]>0;
}

-(void)commandWithParams:(NSMutableDictionary*)params onCompletion:(JSONResponseBlock)completionBlock
{
NSMutableURLRequest *apiRequest =
[self multipartFormRequestWithMethod:@"POST"
path:@"http://localhost/api/index.php"
parameters:params
constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
//TODO: attach file if needed
}];

AFJSONRequestOperation* operation = [[AFJSONRequestOperation alloc] initWithRequest: apiRequest];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
//success!
completionBlock(responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//failure :(
completionBlock([NSDictionary dictionaryWithObject:[error localizedDescription] forKey:@"error"]);
}];

[operation start];
}

@end

index.php

<?
session_start();

require("lib.php");

header("Content-Type:application/json");

switch ($_POST['command'])
{
case "login":
login($_POST['username'],$_POST['password']);
break;
case "register";
register($_POST['username'],$_POST['password']);
break;
}

lib.php

<?php
$link = mysqli_connect("localhost","root","root");
mysqli_select_db($link,"test");

header("Content-Type:application/json");

function errorJson($msg){
print json_encode(array('error'=>$msg));
exit();
}

function register($user, $pass) {

//check if username exists in the database (inside the "users" table)
$login = query("SELECT username FROM users WHERE username='%s' limit 1", $user);

if (count($login['result'])>0) {

//the username exists, return error to the iPhone app
errorJson('Username already exists');
}

//try to insert a new row in the "users" table with the given username and password
$result = query("INSERT INTO users(username, password) VALUES('%s','%s')", $user, $pass);

if (!$result['error']) {
//registration is susccessfull, try to also directly login the new user
login($user, $pass);
} else {
//for some database reason the registration is unsuccessfull
errorJson('Registration failed');
}

}

CDRegisterViewController.m

- (IBAction)signUp:(id)sender
{
// form fields validation
if (self.usernameField.text.length < 4 || self.passwordField.text.length < 4){
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Your password is too short" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil, nil];
[alertView show];
}

// salt the password
NSString *saltedPassword = [NSString stringWithFormat:@"%@%@", self.passwordField.text, kSalt];

// prepare hashed storage
NSString *hashedPassword = nil;
unsigned char hashedPasswordData[CC_SHA1_DIGEST_LENGTH];

//hash the pass
NSData *data = [saltedPassword dataUsingEncoding:NSUTF8StringEncoding];
if (CC_SHA1([data bytes], [data length], hashedPasswordData)) {
hashedPassword = [[NSString alloc] initWithBytes:hashedPasswordData length:sizeof(hashedPasswordData) encoding:NSASCIIStringEncoding];
} else {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Password couldn't be sent" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil, nil];
[alertView show];
return;
}

// set the parameters to post
NSString *command = @"register";
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:command, @"command",
self.usernameField.text, @"username", hashedPassword, @"password", nil];

// make the call to the api
[[API sharedInstance] commandWithParams:params onCompletion:^(NSDictionary *json)
{
//result returned
NSDictionary* res = [[json objectForKey:@"result"] objectAtIndex:0];

if ([json objectForKey:@"error"]==nil && [[res objectForKey:@"IdUser"] intValue]>0) {
//success
[[API sharedInstance] setUser: res];
[self.presentingViewController dismissViewControllerAnimated:YES completion:nil];

//show message to the user
[[[UIAlertView alloc] initWithTitle:@"Logged in"
message:[NSString stringWithFormat:@"Welcome %@",[res objectForKey:@"username"] ]
delegate:nil
cancelButtonTitle:@"Close"
otherButtonTitles: nil] show];

} else {
//error
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error" message:[json objectForKey:@"error"] delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil, nil];
[alertView show];
}
}];
}

最佳答案

您是否使用诸如 Charles Proxy 之类的分析器查看了进出服务的数据?这将是第一步——弄清楚问题是出在服务器上还是出在 iOS 上。接下来提供一些数据、请求和响应。找到不正确的一端并修复。

关于php - 预期的内容类型 text/json 得到 text/html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21739345/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com