在jQuery中访问Wikipedia API JSON数据

时间:2016-12-25 00:45:53

标签: javascript jquery json mediawiki

$('#searchButton').click(function(){
  var searchInput = "";
  searchInput = document.getElementById('test');
  if(searchInput.value !== ""){
    $.getJSON('https://en.wikipedia.org/w/api.php?action=query&list=search&format=json&srsearch='+searchInput+'&utf8=', function(json){
      alert(json.query.search[0].title);
    });
  }
});

我很困惑为什么Json似乎没有加载到页面中。看起来整个操作都在url处停止,即使我在警报中输入一个字符串也不会运行...

1 个答案:

答案 0 :(得分:1)

您收到此错误是因为CORS没有为您调用mediawiki的来源启用,您可以在此处查看更多内容。

https://www.mediawiki.org/wiki/Manual:CORS

您可以使用dataType: 'jsonp'请求$(document).ready(function() { $('#searchButton').click(function(){ var searchInput = ""; searchInput = document.getElementById('test'); if(searchInput.value !== ""){ $.ajax( { url: 'https://en.wikipedia.org/w/api.php', data: { action: 'query', list: 'search', format: 'json', srsearch: searchInput.value }, dataType: 'jsonp' } ).done( function ( json ) { alert(json.query.search[0].title); } ); } }); });代替<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" id="test" /> <input type="button" id="searchButton" value="Search" />

工作代码

12/24 20:28:53: Launching app
$ adb push C:\Users\sam\Downloads\OracleTheDeciscionMaker\app\build\outputs\apk\app-debug.apk /data/local/tmp/app.com.example.android.oraclethedeciscionmaker
$ adb shell pm install -r "/data/local/tmp/app.com.example.android.oraclethedeciscionmaker"
Success


$ adb shell am start -n "app.com.example.android.oraclethedeciscionmaker/app.com.example.android.oraclethedeciscionmaker.HomeScreen" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Client not ready yet..Waiting for process to come online
Waiting for process to come online
Connected to process 4122 on device emulator-5554
W/System: ClassLoader referenced unknown path: /data/app/app.com.example.android.oraclethedeciscionmaker-2/lib/x86
W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
I/art: Background sticky concurrent mark sweep GC freed 21167(2MB) AllocSpace objects, 14(264KB) LOS objects, 0% free, 13MB/13MB, paused 3.199ms total 320.293ms
I/art: Background partial concurrent mark sweep GC freed 2195(133KB) AllocSpace objects, 3(72KB) LOS objects, 23% free, 13MB/17MB, paused 3.735ms total 159.374ms
I/OpenGLRenderer: Initialized EGL, version 1.4
D/OpenGLRenderer: Swap behavior 1
E/EGL_emulation: tid 4150: eglSurfaceAttrib(1174): error 0x3009 (EGL_BAD_MATCH)
W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xb07fd4a0, error=EGL_BAD_MATCH
I/Choreographer: Skipped 78 frames!  The application may be doing too much work on its main thread.
I/Choreographer: Skipped 72 frames!  The application may be doing too much work on its main thread.
D/AndroidRuntime: Shutting down VM


                  --------- beginning of crash
E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: app.com.example.android.oraclethedeciscionmaker, PID: 4122
                  java.lang.IllegalStateException: Could not execute method for android:onClick
                      at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
                      at android.view.View.performClick(View.java:5637)
                      at android.view.View$PerformClick.run(View.java:22429)
                      at android.os.Handler.handleCallback(Handler.java:751)
                      at android.os.Handler.dispatchMessage(Handler.java:95)
                      at android.os.Looper.loop(Looper.java:154)
                      at android.app.ActivityThread.main(ActivityThread.java:6119)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
                   Caused by: java.lang.reflect.InvocationTargetException
                      at java.lang.reflect.Method.invoke(Native Method)
                      at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
                      at android.view.View.performClick(View.java:5637) 
                      at android.view.View$PerformClick.run(View.java:22429) 
                      at android.os.Handler.handleCallback(Handler.java:751) 
                      at android.os.Handler.dispatchMessage(Handler.java:95) 
                      at android.os.Looper.loop(Looper.java:154) 
                      at android.app.ActivityThread.main(ActivityThread.java:6119) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 
                   Caused by: java.lang.ArrayIndexOutOfBoundsException: length=4; index=4
                      at app.com.example.android.oraclethedeciscionmaker.HomeScreen.onClick(HomeScreen.java:23)
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
                      at android.view.View.performClick(View.java:5637) 
                      at android.view.View$PerformClick.run(View.java:22429) 
                      at android.os.Handler.handleCallback(Handler.java:751) 
                      at android.os.Handler.dispatchMessage(Handler.java:95) 
                      at android.os.Looper.loop(Looper.java:154) 
                      at android.app.ActivityThread.main(ActivityThread.java:6119) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 
Application terminated.
package app.com.example.android.oraclethedeciscionmaker;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class HomeScreen extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_screen);
    }

    public void onClick(View view){

        // Setting up an array of 5 random number generators between 0 and 1
        int[] guesser = new int[4];
        guesser[0] = (int) Math.random();
        guesser[1] = (int) Math.random();
        guesser[2] = (int) Math.random();
        guesser[3] = (int) Math.random();
        guesser[4] = (int) Math.random();

        // 0 out of 0
        if(guesser[0] == 0 && guesser[1] == 0 && guesser[2] == 0 && guesser[3] == 0 && guesser[4] == 0){
            Intent intent = new Intent(HomeScreen.this, ZeroOfFive.class);
            startActivity(intent);
            // If this statement is true go to this activity
        }
        // 1 out of 5
        else if(guesser[0] == 0 && guesser[1] == 0 && guesser[2] == 0 && guesser[3] == 0 && guesser[4] == 1
             || guesser[0] == 0 && guesser[1] == 0 && guesser[2] == 0 && guesser[3] == 1 && guesser[4] == 0
             || guesser[0] == 0 && guesser[1] == 0 && guesser[2] == 1 && guesser[3] == 0 && guesser[4] == 0
             || guesser[0] == 0 && guesser[1] == 1 && guesser[2] == 0 && guesser[3] == 0 && guesser[4] == 0
             || guesser[0] == 1 && guesser[1] == 0 && guesser[2] == 0 && guesser[3] == 0 && guesser[4] == 0){
            Intent intent = new Intent(HomeScreen.this, OneOfFive.class);
            startActivity(intent);
            // If this statement is true go to this activity
        }
        //2 out of 5
        else if(guesser[0] == 0 && guesser[1] == 0 && guesser[2] == 0 && guesser[3] == 1 && guesser[4] == 1
             || guesser[0] == 0 && guesser[1] == 0 && guesser[2] == 1 && guesser[3] == 0 && guesser[4] == 1
             || guesser[0] == 0 && guesser[1] == 1 && guesser[2] == 0 && guesser[3] == 0 && guesser[4] == 1
             || guesser[0] == 1 && guesser[1] == 0 && guesser[2] == 0 && guesser[3] == 0 && guesser[4] == 1
             || guesser[0] == 1 && guesser[1] == 1 && guesser[2] == 1 && guesser[3] == 1 && guesser[4] == 0
             || guesser[0] == 0 && guesser[1] == 1 && guesser[2] == 0 && guesser[3] == 1 && guesser[4] == 0
             || guesser[0] == 1 && guesser[1] == 0 && guesser[2] == 0 && guesser[3] == 1 && guesser[4] == 0
             || guesser[0] == 1 && guesser[1] == 0 && guesser[2] == 0 && guesser[3] == 1 && guesser[4] == 0
             || guesser[0] == 0 && guesser[1] == 1 && guesser[2] == 1 && guesser[3] == 0 && guesser[4] == 0
             || guesser[0] == 1 && guesser[1] == 0 && guesser[2] == 1 && guesser[3] == 0 && guesser[4] == 0
             || guesser[0] == 1 && guesser[1] == 1 && guesser[2] == 0 && guesser[3] == 0 && guesser[4] == 0){
            Intent intent = new Intent(HomeScreen.this, TwoOfFive.class);
            startActivity(intent);
            // If this statement is true go to this activity
        }
        //3 out of 5
        else if(guesser[0] == 0 && guesser[1] == 0 && guesser[2] == 1 && guesser[3] == 1 && guesser[4] == 1
             || guesser[0] == 0 && guesser[1] == 1 && guesser[2] == 0 && guesser[3] == 1 && guesser[4] == 1
             || guesser[0] == 1 && guesser[1] == 0 && guesser[2] == 0 && guesser[3] == 1 && guesser[4] == 1
             || guesser[0] == 0 && guesser[1] == 1 && guesser[2] == 1 && guesser[3] == 1 && guesser[4] == 0
             || guesser[0] == 1 && guesser[1] == 0 && guesser[2] == 1 && guesser[3] == 1 && guesser[4] == 0
             || guesser[0] == 1 && guesser[1] == 1 && guesser[2] == 0 && guesser[3] == 1 && guesser[4] == 0
             || guesser[0] == 1 && guesser[1] == 1 && guesser[2] == 1 && guesser[3] == 0 && guesser[4] == 0){
            Intent intent = new Intent(HomeScreen.this, ThreeOfFive.class);
            startActivity(intent);
            // If this statement is true go to this activity
        }
        //4 out of 5
        else if(   guesser[0] == 0 && guesser[1] == 1 && guesser[2] == 1 && guesser[3] == 1 && guesser[4] == 1
                || guesser[0] == 1 && guesser[1] == 0 && guesser[2] == 1 && guesser[3] == 1 && guesser[4] == 1
                || guesser[0] == 1 && guesser[1] == 1 && guesser[2] == 1 && guesser[3] == 1 && guesser[4] == 0
                || guesser[0] == 1 && guesser[1] == 1 && guesser[2] == 1 && guesser[3] == 0 && guesser[4] == 1){
            Intent intent = new Intent(HomeScreen.this, FourOfFive.class);
            startActivity(intent);
            // If this statement is true go to this activity

        }
        //5 out of 5
        else {
            Intent intent = new Intent(HomeScreen.this, FiveOfFive.class);
            startActivity(intent);
        }
    }
}