dornc9470 2019-01-18 18:48
浏览 199
已采纳

AJAX请求无效。 错误400和CORS策略解决方案无法正常工作

The task is to validate a specific ID that a new customer is typing into an HTML text input field during registration. The new customer is allowed to register only if 5 numbers are in the input field and the number is given away to an existing member (also a alert box shall pop up).

So this is in my .js file. The following parameter reg_sub is the value of the input from the user.

function checkSubscriberIdValidation( reg_sub )
{   

    if( reg_sub.length == 5)
    {
        console.log(reg_sub);
        jQuery.ajax({
        url: specialStrings.ajax_url,
        type: "GET",
        data: ({action: 'checkCUID', cuID: reg_sub}),
    success: function(result){
            console.log(result);
    },
    error: function(e){
        console.log(e);
    }); 
    }
}

I am trying to call the following code from the file named : checkCUID.php

<?php
if( isset($_GET['cuID']) )
{
    if(!defined("ABSPATH")){
        define( 'ABSPATH', $_SERVER['DOCUMENT_ROOT'].'/');
    }
    try
    {   
        require_once dirname(__FILE__).'/../library.php';
        $service  = Company::getService('CustomerService');
        $response = $service->validateCUID($_GET['cuID']);

        if($response == false)
    {
        return;
        {
        else
        {
            echo '0'; 
        }
    }
    catch(Exception $e)
    {
        echo $e->getMessage();
    }
}
else
{
    echo '0';
}

So the function validateCUID(); is working because I am also testing it with SOAP UI and I get always true back (if I do insert a valid ID).

So this is my response in the Console (a number and a GET 400 ERROR):

99999
GET https://domain.de/wp-admin/admin-ajax.php?action=checkCUID&cuID=99999 400 

I tried to insert into the:

jQuery.ajax({
    contentType: "application/json; charset=utf-8", 
    dataType: "json",
})

but now I got the Error:

Access to XMLHttpRequest at 'https://domain.de/wp-admin/admin-ajax.php?action=checkCUID&cuID=99999' from origin 'http://domain.de' has been blocked by CORS policy: Request header field content-type is not allowed by Access-Control-Allow-Headers in preflight response.

I tried to change the content-type to text/html like its showing in the admin-ajax.php. I saw that the origin domain starts with http and not with https and checked the home and siteurl in the phpMyAdmin and it is https there.

Does anyone know a solution? My guess is: my code is a crap, I am still studying and I am so tired. I am so sorry for that long issue^^!

  • 写回答

1条回答 默认 最新

  • dpqjvoq9033 2019-01-18 21:17
    关注

    I noticed that you are using wordpress, try to use this on a plugin or in your template functions.php:

    add_filter( 'allowed_http_origins', 'add_allowed_origins' );
    function add_allowed_origins( $origins ) {
        $origins[] = 'https://site1.example.com';
        $origins[] = 'https://site2.example.com';
        return $origins;
    }
    

    where each url is the frontend URL that are allowed to query the server.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?