Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AsyncCallbackJsonWebHandler does not trigger #402

Open
poengAlex opened this issue Aug 24, 2018 · 6 comments

Comments

Projects
None yet
4 participants
@poengAlex
Copy link

commented Aug 24, 2018

The AsyncCallbackJsonWebHandler does not trigger upon url request.

AsyncCallbackJsonWebHandler* handler = new AsyncCallbackJsonWebHandler("/endpoint", [](AsyncWebServerRequest *request, JsonVariant &json) {
	JsonObject& jsonObj = json.as<JsonObject>();
	jsonObj.prettyPrintTo(Serial);
	request->send(200, "application/json", "{test: \"ok\"}");
});
server.addHandler(handler);
$.ajax({ 
        type: 'GET', 
        url: '/endpoint', 
        data: { 
            txt: "test",
            nr: 123,
            digi: true
        }, 
        dataType: 'json',
        contentType: 'application/json; charset=UTF-8'
    })
    .done(function(data) {
        console.log( "success" );
        console.log(data);
        $scope.checkConnection();
    })
    .fail(function(data) {
        console.log( "error" );
        console.log(data);
    })
    .always(function() {
        console.log( "complete" );
    });
_HEADER[Host]: 192.168.0.193
_HEADER[Connection]: keep-alive
_HEADER[Pragma]: no-cache
_HEADER[Cache-Control]: no-cache
_HEADER[Accept]: application/json, text/javascript, */*; q=0.01
_HEADER[X-Requested-With]: XMLHttpRequest
_HEADER[User-Agent]: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36 OPR/54.0.2952.64
_HEADER[Content-Type]: application/json; charset=UTF-8
_HEADER[Referer]: http://192.168.0.193/
_HEADER[Accept-Encoding]: gzip, deflate
_HEADER[Accept-Language]: en-US,en;q=0.9
Host: 192.168.0.193
NOT_FOUND: GET http://192.168.0.193/endpoint
_GET[SSID]: test
_GET[password]: 200
_GET[digi]: true
@brunohorta82

This comment has been minimized.

Copy link

commented Sep 17, 2018

Hello I have the same issue I think it's because cors :(,

@poengAlex

This comment has been minimized.

Copy link
Author

commented Sep 17, 2018

Hello I have the same issue I think it's because cors :(,

Why? The JS file is served from the same server as the ajax request.

@brunohorta82

This comment has been minimized.

Copy link

commented Sep 17, 2018

yes, if I use Postman, works fine, but if I use jQuery no :(

@brunohorta82

This comment has been minimized.

Copy link

commented Sep 17, 2018

Finally I solved the problem :)
change this contentType: 'application/json; charset=UTF-8' to contentType: 'application/json'

function storeSwitch(id, _switch) {
    const someUrl = config.baseUrl + "/save-switch?id=" + id;
    $.ajax({
        type: "POST",
        url: someUrl,
        dataType: "json",
        contentType: "application/json",
        data: JSON.stringify(_switch),
        success: function (response) {

        },
        error: function () {

        }, complete: function () {

        },
        timeout: 2000
    });
}

:)

@jnicolson

This comment has been minimized.

Copy link

commented Jan 29, 2019

I just had the same problem, but I resolved it by modifying

if (!request->contentType().equalsIgnoreCase(JSON_MIMETYPE))

changing equalsIgnoreCase to startsWith (both Arduino string functions). Probably isn't the best solution (be better to parse the Content-Type properly and just get the first bit to compare) but it let me continue

@warlord0

This comment has been minimized.

Copy link

commented Mar 28, 2019

I just had the same problem, but I resolved it by modifying

ESPAsyncWebServer/src/AsyncJson.h

Line 126 in 95dedf7

if (!request->contentType().equalsIgnoreCase(JSON_MIMETYPE))
changing equalsIgnoreCase to startsWith (both Arduino string functions). Probably isn't the best solution (be better to parse the Content-Type properly and just get the first bit to compare) but it let me continue

Good catch. Saved me some work. In my case I was able to modify my client call to pass only the mimetype with no code page. Meaning I didn't have to modify the AsyncJson.h file. But I can see this being a problem where I don't control the client script.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.