Skip to content

Forum in maintenance, we will back soon 🙂

Notifications
Clear all

Ajax error

13 Posts
4 Users
2 Reactions
282 Views
Lawal Ridwan
(@leksidenation)
Posts: 2
Active Member
Topic starter
 

Am having issue with the ajax 

 

is show this error

 

image

Free Online Web Tools
500+ Premium Chatgpt Prompt Templates
Prompt Jedi: 40,000+ Premium Chatgpt Prompts V3

 
Posted : 01/21/2024 7:22 am
Topic Tags
Hasan Aboul Hasan
(@admin)
Posts: 1253
Member Admin
 

Your function is working perfectly; this is a security error from the browser.

I tested it with another Client, and it worked:

Screenshot 2024 01 21 111337

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 
 
Posted : 01/21/2024 9:14 am
Dim2001
(@dassa)
Posts: 99
Member
 

Mr Hassan, I`m having an error when testing the PHP script.

1
2

 cannot figure out why this happening?

 
Posted : 03/16/2024 10:08 am
SSAdvisor
(@ssadvisor)
Posts: 1139
Noble Member
 

@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

 
Posted : 03/16/2024 2:03 pm
Hasan Aboul Hasan
(@admin)
Posts: 1253
Member Admin
 

@dassa it shows that your site has a critical error. 

check the snippet, it seems that there is a problem. can you share the snippet you are trying to test please?

 
Posted : 03/16/2024 3:10 pm
Dim2001 reacted
Dim2001
(@dassa)
Posts: 99
Member
 

@ssadvisor namecheap

 
Posted : 03/17/2024 2:12 am
Dim2001
(@dassa)
Posts: 99
Member
 

@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).

3

 

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');
 
Posted : 03/17/2024 2:23 am
SSAdvisor
(@ssadvisor)
Posts: 1139
Noble Member
 

@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

 
Posted : 03/17/2024 4:02 am
Dim2001
(@dassa)
Posts: 99
Member
 

@ssadvisor Just now I test it again and now working fine. 😀

 
Posted : 03/17/2024 7:00 am
SSAdvisor reacted
Dim2001
(@dassa)
Posts: 99
Member
 

again it happed.

8
7

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.

 
Posted : 03/17/2024 9:32 am
SSAdvisor
(@ssadvisor)
Posts: 1139
Noble Member
 

@dassa this solution looks correct to me.

Regards,
Earnie Boyd, CEO
Seasoned Solutions Advisor LLC
Schedule 1-on-1 help
Join me on Slack

 
Posted : 03/17/2024 1:43 pm
Dim2001
(@dassa)
Posts: 99
Member
 

@ssadvisor let see when it complete

 
Posted : 03/17/2024 2:38 pm
Hasan Aboul Hasan
(@admin)
Posts: 1253
Member Admin
 

The user ID should be defined before accessing. 

is it working now?

 
Posted : 03/17/2024 3:59 pm
Share: