-
No Response From My WP saas Backend
Help! Help! Help!
Hello! please i have a problem with my wp saas backend, for some reasons it is refusing to give feedback.When i used Thunder Client, i got a “400 Bad Request”,
When i try to call it via my UI, i get this error: “Access to fetch at ‘http://test-site.local/wp-admin/admin-ajax.php’ from origin ‘null’ has been blocked by CORS policy: No ‘Access-Control-Allow-Origin’ header is present on the requested resource. If an opaque response serves your needs, set the request’s mode to ‘no-cors’ to fetch the resource with CORS disabled.” — even though my code has CORS headers.
Here is my code:
function openai_SOCFHC() {
// Add CORS headers
header(‘Access-Control-Allow-Origin: *’); // Allows all origins
header(‘Access-Control-Allow-Methods: GET, POST, OPTIONS’); // Specifies the allowed methods
header(‘Access-Control-Allow-Headers: Content-Type, Authorization’); // Specifies the allowed headers
// Get the facts and suggestedOffence from the AJAX request
$facts = $_POST[‘facts’];
$suggestedOffence = $_POST[‘suggestedOffence’];
$prompt = “You are a highly experienced lawyer with 20 years of expertise in legal analysis. Your task is to thoroughly assess the following facts: {” . $facts . “}.
If the user has suggested an offence:
Analyze the provided facts to determine if they establish the elements necessary to ground the suggested offence: {” . $suggestedOffence . “}.
Provide a concise list of the essential elements required for a conviction based on the suggested offence.
Additionally, if the facts support more than one offence, advise on all appropriate offences that could be instituted.
If the user has not suggested an offence:
Analyze the facts and recommend the most appropriate offence(s) that could be pursued based on your assessment.
Your output should be direct and concise. If the user does not provide a recognizable offence, do not provide an answer.”;
// OpenAI API URL and key
$api_url = ‘https://api.openai.com/v1/chat/completions’;
$api_key = ‘skJ9BIA’; // Replace with your actual OpenAI API key
// Headers for the OpenAI API
$headers = [
‘Content-Type’ => ‘application/json’,
‘Authorization’ => ‘Bearer ‘ . $api_key
];
// Body for the OpenAI API
$body = [
‘model’ => ‘gpt-3.5-turbo’,
‘messages’ => [[‘role’ => ‘user’, ‘content’ => $prompt]],
‘temperature’ => 0.7
];
// Args for the WordPress HTTP API
$args = [
‘method’ => ‘POST’,
‘headers’ => $headers,
‘body’ => json_encode($body),
‘timeout’ => 120
];
// Send the request
$response = wp_remote_request($api_url, $args);
// Handle the response
if (is_wp_error($response)) {
$error_message = $response->get_error_message();
wp_send_json_error(“Something went wrong: $error_message”);
} else {
$body = wp_remote_retrieve_body($response);
$data = json_decode($body, true);
if (json_last_error() !== JSON_ERROR_NONE) {
wp_send_json_error(‘Invalid JSON in API response’);
} elseif (!isset($data[‘choices’]) || !isset($data[‘choices’][0][‘message’][‘content’])) {
wp_send_json_error(‘API request failed. Response: ‘ . $body);
} else {
$content = $data[‘choices’][0][‘message’][‘content’];
// Deduct points
$deducted = mycred_subtract(
“Tool Usage”,
$user_id,
$cost,
“Point deduction for tool usage”,
time()
);
if ($deducted === false) {
wp_send_json([‘success’ => false, ‘message’ => ‘Failed to deduct points.’]);
} else {
wp_send_json([‘success’ => true, ‘message’ => ‘Points deducted successfully.’]);
}
}
wp_send_json_success($content); // Changed from wp_send_json to wp_send_json_success
}
}
// Always die in functions echoing AJAX content
wp_die();
}
add_action(‘wp_ajax_openai_SOCFHC’, ‘openai_SOCFHC’);
add_action(‘wp_ajax_nopriv_openai_SOCFHC’, ‘openai_SOCFHC’);
Sorry, there were no replies found.
Log in to reply.