我已经扭动我的代码几个小时了,但是没有找到在laravel url()
中放入javascript变量的正确方法。每当我将其作为参数放置在url()内时,该变量就被当作字符串。下面是我的代码。请帮忙。
javascript文件
function generateSchedule( id, loan_type ){
var txt_url;
if( loan_type == "Business" ){
txt_url = 'addamortbusinessloan';
}else{
txt_url = 'addamortization';
}
$.post('{{ url("'+txt_url+'") }}', {'loanId':id}, function(data){
},"json");
}
这不起作用{{ url("'+txt_url+'") }}
。我一直在扭曲引号的位置,但根本没有运气。其结果是http://localhost:8000/txt_url
。我希望它像这样http://localhost:8000/addamortbusinessloan
或http://localhost:8000/addamortization
答案 0 :(得分:2)
您无需将其放在逗号中,无需逗号即可正常工作。选中
function generateSchedule( id, loan_type ){
var txt_url;
if( loan_type == "Business" ){
txt_url = "addamortbusinessloan";
}else{
txt_url = "addamortization";
}
var base_url="http://localhost:8000/"
$.post( base_url+txt_url, {'loanId':id}, function( data ) {
}, "json");
}
答案 1 :(得分:0)
使用此Blade指令以简单的方式将变量导出到JS:spatie/laravel-blade-javascript
答案 2 :(得分:0)
实际上您不能,但是如果需要,可以像这样在app.blade文件中定义全局变量
import React from "react"
import { withFormik, Form, Field } from "formik"
import axios from "axios"
const FormComponent = ({ values, errors }) => (
<div>
<Form>
<label># </label>
<Field className="HashtagInput" type="text" name="hashtag" />
<button className="SearchBtn" type="submit"></button>
</Form>
<p className="SearchResult">{values.hashtagSearchResult}</p>
</div>
)
const FormikApp = withFormik({
mapPropsToValues() {
return {
hashtag: "",
hashtagSearchResult: [], //
}
},
handleSubmit(values) {
console.log("$ Search button clicked!")
let hashtag = values.hashtag
let hashtagResult = []
let url = "https://jsonplaceholder.typicode.com/todos/" + hashtag + "/"
axios.get(url).then(res => {
let hashtags = searchHashtags(res.data)
...
console.log("$ Hashtag result - " + hashtagResult)
values.hashtagSearchResult = hashtagResult;
})
...
})(FormComponent)
或者您可以在var BASE_URL='{{url('')}}'
</script>
的meta标签中定义它的另一种方式:
并在您的javascript文件中,您可以通过jquery进行获取
head
答案 3 :(得分:-1)
function generateSchedule( id, loan_type ){
var txt_url;
if( loan_type == "Business" ){
txt_url = '/addamortbusinessloan';
}else{
txt_url = '/addamortization';
}
var url = "{{ url('') }}" + txt_url;
console.log(url);
$.post(url, {'loanId':id}, function(data){
},"json");
}