Forum in maintenance, we will back soon 🙂
Ajax error
Am having issue with the ajax
is show this error
Free Online Web Tools
500+ Premium Chatgpt Prompt Templates
Prompt Jedi: 40,000+ Premium Chatgpt Prompts V3
Your function is working perfectly; this is a security error from the browser.
I tested it with another Client, and it worked:
Please add the following codes in the return_fixed_titles function.
// 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
Mr Hassan, I`m having an error when testing the PHP script.
cannot figure out why this happening?
@dassa are you using Cloudflare for your DNS?
Regards,
Earnie Boyd, CEO
Seasoned Solutions Advisor LLC
Schedule 1-on-1 help
Join me on Slack
@admin I just use the snippet got from your course. Did not change anything. Still, I`m in the learning phase. So I add this to test with vs cords. Also, it is automatically inactive because of some error. (shown in the picture).
function openai_generate_text_basic() { // 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 topic from the AJAX request $topic = $_POST['topic']; $usage = $_POST['usage']; $prompt = "As an expert copywriter specialized in hook generation, your task is to Generate 3 hooks for the following topic: {" . $topic . "} for using it in a: {" . $usage . "}. The output should be a Only The list of Hooks with the type. The Hooks List:\n\n"; // OpenAI API URL and key $api_url = 'https://api.openai.com/v1/chat/completions'; $api_key = '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'; // 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']; mycred_subtract("Hook Generator", $user_id, 5, 'Point deduction for using hook Generation', time()); wp_send_json($content); } } // Always die in functions echoing AJAX content wp_die(); } add_action('wp_ajax_openai_generate_text_basic', 'openai_generate_text_basic'); add_action('wp_ajax_nopriv_openai_generate_text_basic', 'openai_generate_text_basic');
@dassa if you hover over the red "i" in a circle (or maybe you need to click it) what information do you get about the disabled snippet?
Regards,
Earnie Boyd, CEO
Seasoned Solutions Advisor LLC
Schedule 1-on-1 help
Join me on Slack
@ssadvisor Just now I test it again and now working fine. 😀
again it happed.
Then I ask chatgpt and it says add below line to define user_id
// Retrieve the current user's ID $user_id = get_current_user_id(); // Subtract points using mycred_subtract function mycred_subtract("Hook Generator", $user_id, 5, 'Point deduction for using hook Generation', time());
now it work fine. but is it correct? does it effect to the mycred.
@dassa this solution looks correct to me.
Regards,
Earnie Boyd, CEO
Seasoned Solutions Advisor LLC
Schedule 1-on-1 help
Join me on Slack
The user ID should be defined before accessing.
is it working now?