我已经得到了这段代码:
$(document).ready(function () {
$.getJSON('http://localhost:5000/', function (response) {
console.log(response);
});
});
localhost:5000是一个flask/python
脚本,它返回json
,如:
{
"data": [
0,
0,
0,
我得到了:
$.getJSON is not a function TypeError: $.getJSON is not a function
我可以开始解开whoolball的任何提示吗?
谢谢!
编辑:
HTML:
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.1.1.slim.min.js"></script>
<script src="lib/main.js"></script>
</head>
<body>
</body>
</html>
lib/main.js
是document.ready
所在的位置。
谢谢!
答案 0 :(得分:93)
您似乎正在使用<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:bind="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="bindingPlanet"
type="au.com.chrisli.spinnertwowaydatabindingdemo.BindingPlanet"/>
<variable
name="spinAdapterPlanet"
type="android.widget.ArrayAdapter"/>
</data>
<RelativeLayout
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
...>
<android.support.v7.widget.AppCompatSpinner
android:id="@+id/spin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
style="@style/Base.Widget.AppCompat.Spinner.Underlined"
bind:selectedPlanet="@={bindingPlanet.obvSelectedPlanet_}"
app:adapter="@{spinAdapterPlanet}"/>
...(not relevant content omitted for simplicity)
</RelativeLayout>
</layout>
版public final ObservableField<Planet> obvSelectedPlanet_ = new ObservableField<>(); //for simplicity, we use a public variable here
private static class SpinPlanetOnItemSelectedListener implements AdapterView.OnItemSelectedListener {
private Planet initialSelectedPlanet_;
private InverseBindingListener inverseBindingListener_;
public SpinPlanetOnItemSelectedListener(Planet initialSelectedPlanet, InverseBindingListener inverseBindingListener) {
initialSelectedPlanet_ = initialSelectedPlanet;
inverseBindingListener_ = inverseBindingListener;
}
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
if (initialSelectedPlanet_ != null) {
//Adapter is not ready yet but there is already a bound data,
//hence we need to set a flag so we can implement a special handling inside the OnItemSelectedListener
//for the initial selected item
Integer positionInAdapter = getPlanetPositionInAdapter((ArrayAdapter<Planet>) adapterView.getAdapter(), initialSelectedPlanet_);
if (positionInAdapter != null) {
adapterView.setSelection(positionInAdapter); //set spinner selection as there is a match
}
initialSelectedPlanet_ = null; //set to null as the initialization is done
} else {
if (inverseBindingListener_ != null) {
inverseBindingListener_.onChange();
}
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {}
}
@BindingAdapter(value = {"bind:selectedPlanet", "bind:selectedPlanetAttrChanged"}, requireAll = false)
public static void bindPlanetSelected(final AppCompatSpinner spinner, Planet planetSetByViewModel,
final InverseBindingListener inverseBindingListener) {
Planet initialSelectedPlanet = null;
if (spinner.getAdapter() == null && planetSetByViewModel != null) {
//Adapter is not ready yet but there is already a bound data,
//hence we need to set a flag in order to implement a special handling inside the OnItemSelectedListener
//for the initial selected item, otherwise the first item will be selected by the framework
initialSelectedPlanet = planetSetByViewModel;
}
spinner.setOnItemSelectedListener(new SpinPlanetOnItemSelectedListener(initialSelectedPlanet, inverseBindingListener));
//only proceed further if the newly selected planet is not equal to the already selected item in the spinner
if (planetSetByViewModel != null && !planetSetByViewModel.equals(spinner.getSelectedItem())) {
//find the item in the adapter
Integer positionInAdapter = getPlanetPositionInAdapter((ArrayAdapter<Planet>) spinner.getAdapter(), planetSetByViewModel);
if (positionInAdapter != null) {
spinner.setSelection(positionInAdapter); //set spinner selection as there is a match
}
}
}
@InverseBindingAdapter(attribute = "bind:selectedPlanet", event = "bind:selectedPlanetAttrChanged")
public static Planet captureSelectedPlanet(AppCompatSpinner spinner) {
return (Planet) spinner.getSelectedItem();
}
版本的slim
没有方法jquery
,这就是您收到此错误的原因。
请使用完整版getJSON
,而不是以下链接。
https://code.jquery.com/jquery-3.1.1.min.js
<{>jquery
Slim
版jquery
排除了ajax
,animations effects
等
答案 1 :(得分:1)
因此,当jQuery更新/弃用/停用某些API时,将代码库从jQuery v1.x +迁移到v3.0 +时会发生这种情况。
我建议使用jQuery Migrate来解决这个问题以及其他问题:
通过CDN来到这里:
https://cdnjs.com/libraries/jquery-migrate
如果使用Gulp / Grunt,您可以使用
导入项目 npm install --save jquery jquery-migrate
Github存储库 - https://github.com/jquery/jquery-migrate
阅读有关jQuery v3.0 +的更多信息.. http://blog.jquery.com/2016/06/09/jquery-3-0-final-released/
答案 2 :(得分:1)
function cinta(){
$.getJSON('http://localhost:5000/', function (response) {
console.log(response);
});
}
cinta();
$(document).ready(function () {
console.log('yesss');
});
这项工作适合我在python烧瓶中
答案 3 :(得分:0)
您还可以将 Fetch
与 async
和 await
一起使用:
async function getData(){
const response = await fetch( "http://localhost:5000/"
);
return response.json();
}
getData().then((data) => {
//... your code
})