I
I
Igor Mavlikhanov2017-02-28 01:03:07
CORS
Igor Mavlikhanov, 2017-02-28 01:03:07

CORS | Authorization | Electron how to make a request?

Hello.
I'm trying to make an application using Electron.
I can't figure out what the problem is. But I guess that the problem may be that I'm trying to log in to the worpress site that is installed in the folder.
site: example.ru/example1/wp-json/wp/v2

3f12f3d94e08476baf75e32c1c8a7cb8.png
these are the headers I receive and send
30c448a2b83c4ec893cfdbfab3eca82e.png
97d3ae1dd1f24f229b260517100ac65d.png

here and here is the request I send

$.ajax({
    url: 'http://diamantweb.com/user_data/igor/wp/qtranslate-x-work/wp-json/wp/v2/posts/1',
    method: 'DELETE',
    crossDomain: true,
    beforeSend: function ( xhr ) {
        xhr.setRequestHeader( 'Authorization', 'Basic ' + Base64.encode( 'хххххх:хххххх' ) );
    },
    success: function( data, txtStatus, xhr ) {
        console.log( data );
        console.log( xhr.status );
    }
});
});


Questions:
1. Can it be the reason that the site is installed in a folder?
2. I can not remove one heading.
Access-Control-Allow-Origin:* // этот ставлю я
Access-Control-Allow-Origin:  // этот отдаёт сервер


Help me please.

Worth the Basic-Auth plugin

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
E, 2017-02-28
@Gori4ka

It was only after installing the plugin that I was able to delete the post/page via the api. Rest client (firefox add-on) always gives "401 Unauthorized". But through curl, the post/page was deleted.
Try it, it might help.
And now the second part of your question:

add_action('rest_api_init', function () {

    remove_filter('rest_pre_serve_request', 'rest_send_cors_headers');
    add_filter('rest_pre_serve_request', function ($value) {
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE');
        header('Access-Control-Allow-Credentials: true');

        return $value;

    });
}, 15);

174d879880ae436da3e7699821feac46.png

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question