使用浏览器时从ajax调用中获取值,但在Postman中则不是

问题描述

我需要解码一个字符串,并告诉它是否成功。我需要先调用/dummy.do来对someString进行编码。 /displayapp将使用其输出来解码并在解码的字符串中找到某些内容,然后返回shortString和成功值。

当我在浏览器中运行此代码时,我可以从/dummy.do/displayapp获得响应并呈现。 /dummy.do的响应为“长字符串”,而/displayapp的响应为“短字符串”和“ true”。我什至可以在日志中获取“主机名未提供”。

当我在Postman中测试/dummy.do时,我可以得到“长字符串”。但是,当我在Postman中测试/displayapp时,我无法输出任何内容。 两个响应状态均为200,所以我不知道为什么它没有显示。如果已将其部署在我们的服务器中,则/displayapp

中将出现500 POST错误

SampleUI.tsx

axios.post(contextpath + '/dummy.do',someString,{
        transformRequest: [
            (data,headers) => {
                const formData = new FormData();
                formData.set('someString',someString);
                return formData;
            }
        ]
    })
    .then(function (response) {
        console.log('response dummy',response)
        let longString = response.data.longString
        let hostname = window.location.hostname
        axios.post(contextpath + '/displayapp',{ longString,hostname },{
                transformRequest: [
                    (data,headers) => {
                        const formData = new FormData();
                        formData.set('longString',longString);
                        formData.set('hostname',hostname);
                        return formData;
                    }
                ]
            })
            .then(function (response) {
                console.log('response displayapp',response)
                if (hostname == 'prodUrl.here') {
                    if (response.data.success == 'true') {
                        // some code here
                    }
                    else {
                        // some code here
                    }
                }
                else {
                    console.log('hostname not prod')
                    ReactDOM.render(<Main />,document.getElementById('root'));
                }

            })
    })

SampleController.java

@RestController
public class SampleController {

    @RequestMapping(value = "/dummy.do",method = RequestMethod.POST)
    public Map<String,Object> dummy(HttpServletRequest request,HttpServletResponse response,String someString) throws Exception {
        HashMap<String,Object> resultMap = new HashMap<String,Object>();

        // will do some encoding of someString and will assign it to longString
        // say value of longString is "Long String"

        resultMap.put("longString",longString);

        return resultMap;
    }

    @RequestMapping(value = "/displayapp")
    public Map<String,Object> displayapp(HttpServletRequest request,String longString,String hostname) throws Exception {
        HashMap<String,Object>();

        // will do some decoding of longString,find something from the string and will assign it to shortString
        // say value of shortString is "Short String"

        resultMap.put("shortString",shortString);
        resultMap.put("success","true");

        return resultMap;
    }
    
}```

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)