简体   繁体   English

在 javascript 的后台使用 post 方法打开 url

[英]Open url with post method in background in javascript

I want to open a ( https://example.com/core/login ) with post method when user tried to login for send login datas to a php file with post method in background of login page and get result with javascript.当用户尝试登录以在登录页面的背景中使用 post 方法将登录数据发送到php文件并使用 ZDE9B9ED78D7E2E1ZEFFEE.9 This php file result is in json format and I want to tell to javascript if the 'result' is 'ok' in: {'result':'ok','userid':'65363546'} redirect user to dashboard else show a sample error.这个 php 文件结果是 json 格式,我想告诉 javascript 如果“结果”在: {'result':'ok','userid':'65363546'}样本错误。

You can use fetch api or axios plugin to make a post request and check if the response result equals 'ok' .您可以使用 fetch api 或 axios 插件发出 post 请求并检查响应结果是否等于'ok' On true condition you can use window.location.href = '/path/to/dashboard' on false you can do whatever to generate the error message.在 true 条件下,您可以使用window.location.href = '/path/to/dashboard'在 false 上,您可以执行任何操作来生成错误消息。


const loginData = {
    "email": "x@y.z",
    "password": "xyz@password"
};

const headers = {
    method: 'post',
    body: JSON.stringify(loginData)
}

fetch('https://example.com/core/login', headers).then(function(response) {
    return response.json();
  }).then(function(data) {
    if (data.result == 'ok') {
        window.location.href = "/path/to/dashboard";
    } else {
        alert("Error: show a descriptive error");
    }
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM