ajax全局变量 thymeleaf,html - Ajax Thymeleaf Springboot - Stack Overflow

There are a few issues with your code. First of all, it looks like you're using the same template for both the initial loading of the application, and returning the calculated result.

You should split these two into different calls if you're using AJAX, since one of the goals of AJAX is that you don't need to reload an entire page for one change.

If you need to return a simple value, you should use a separate request method like this:

@GetMapping("/calculation")

@ResponseBody

public int multiply(@RequestParam int input) {

return input * 2; // The calculation

}

What's important to notice here is that I'm using @ResponseBody and that I'm sending the input to this method as a @RequestParam.

Since you will be returning the calculated value directly, you don't need the Model, nor the responseMsg. So you can remove that from your original request mapping.

You can also remove it from your

, since the goal of your code is to use AJAX to fill this element and not to use Thymeleaf. So you can just have an empty element:

Now, there are also a few issues with your Thymeleaf page. As far as I know, '@{/testurl}' is not the valid syntax for providing URLs. The proper syntax would be to use square brackets:

var url = [[@{/calculation}]];

You also have to make sure you change the url to point to the new request mapping. Additionally, this doesn't look as beautiful since it isn't valid JavaScript, the alternative way to write this is:

var url = /*[[ @{/calculation} ]]*/ null;

Now, your script has also a few issues. Since you're using $().load() you must make sure that you have jQuery loaded somewhere (this looks like jQuery syntax so I'm assuming you want to use jQuery).

You also have to send your input parameter somehow. To do that, you can use the event object that will be passed to the doThing() function, for example:

function doThing(evt) {

var url = [[@{/calculation}]];

$("#fill").load(url + '?input=' + evt.target.value);

alert('Horray! Someone wrote "' + this.value + '"!');

}

As you can see, I'm also adding the ?input=, which will allow you to send the passed value to the AJAX call.

Finally, using $().load() isn't the best way to work with AJAX calls unless you try to load partial HTML templates asynchronously. If you just want to load a value, you could use the following code in stead:

$.get({

url: /*[[ @{/calculation} ]]*/ null,

data: { input: evt.target.value }

}).then(function(result) {

$('#fill').text(result);

});

Be aware that $.get() can be cached by browsers (the same applies to $().load() though). So if the same input parameter can lead to different results, you want to use different HTTP methods (POST for example).

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值