AJAX request is not working. Error 400 and CORS Policy solutions not working
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^^!
php ajax wordpress http request
add a comment |
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^^!
php ajax wordpress http request
access your frontend over https, not http, and there won't be a mismatch.
– Dan Farrell
Jan 18 at 18:54
Issue appears to be your content-type header, I would investigate the $.ajax() option 'dataType' and see if setting it to 'json' will help you pass the pre-flight request.
– Jason Sears
Jan 18 at 19:16
add a comment |
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^^!
php ajax wordpress http request
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^^!
php ajax wordpress http request
php ajax wordpress http request
edited Jan 18 at 21:12
Kashif Rafique
732520
732520
asked Jan 18 at 18:48
Alex SchwiftyAlex Schwifty
103
103
access your frontend over https, not http, and there won't be a mismatch.
– Dan Farrell
Jan 18 at 18:54
Issue appears to be your content-type header, I would investigate the $.ajax() option 'dataType' and see if setting it to 'json' will help you pass the pre-flight request.
– Jason Sears
Jan 18 at 19:16
add a comment |
access your frontend over https, not http, and there won't be a mismatch.
– Dan Farrell
Jan 18 at 18:54
Issue appears to be your content-type header, I would investigate the $.ajax() option 'dataType' and see if setting it to 'json' will help you pass the pre-flight request.
– Jason Sears
Jan 18 at 19:16
access your frontend over https, not http, and there won't be a mismatch.
– Dan Farrell
Jan 18 at 18:54
access your frontend over https, not http, and there won't be a mismatch.
– Dan Farrell
Jan 18 at 18:54
Issue appears to be your content-type header, I would investigate the $.ajax() option 'dataType' and see if setting it to 'json' will help you pass the pre-flight request.
– Jason Sears
Jan 18 at 19:16
Issue appears to be your content-type header, I would investigate the $.ajax() option 'dataType' and see if setting it to 'json' will help you pass the pre-flight request.
– Jason Sears
Jan 18 at 19:16
add a comment |
1 Answer
1
active
oldest
votes
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.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54259831%2fajax-request-is-not-working-error-400-and-cors-policy-solutions-not-working%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
add a comment |
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.
add a comment |
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.
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.
answered Jan 18 at 21:17
Walker LeiteWalker Leite
1366
1366
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54259831%2fajax-request-is-not-working-error-400-and-cors-policy-solutions-not-working%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
access your frontend over https, not http, and there won't be a mismatch.
– Dan Farrell
Jan 18 at 18:54
Issue appears to be your content-type header, I would investigate the $.ajax() option 'dataType' and see if setting it to 'json' will help you pass the pre-flight request.
– Jason Sears
Jan 18 at 19:16