为什么这个API调用$ .ajax和$ .post不起作用

时间:2017-10-05 17:23:13

标签: jquery ajax

<!DOCTYPE html>
<html>
<head>
<script src="js/jquery-3.2.1.js"></script>
<script lang="javascript">$.ready(function () {
$.post("https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=BTC,USD,EUR,GBP", function(data) {
  alert(data)

})
}) </script>
</head>
<body>
</body>
</html>

这是为了从cryptocompare API中提取{“BTC”:0.0684,“USD”:294.38,“EUR”:251.24,“GBP”:227.87},然后发出警报,但它什么也没显示。我还检查了devtools,并且

中没有任何传出

4 个答案:

答案 0 :(得分:0)

我认为您正在寻找 <!DOCTYPE html> <html> <head> <script src="js/jquery-3.2.1.js"></script> <script lang="javascript">$(document).ready(function () { $.get("https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=BTC,USD,EUR,GBP", function(data) { alert(data) }) }) </script> </head> <body> </body> </html> 而不是@Override public void onClick (View v){ try { if (ActivityCompat.checkSelfPermission(Activity.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(Activity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, PICK_FROM_GALLERY); } else { Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(galleryIntent, 1); } } catch (Exception e) { e.printStackTrace(); } } 这应该有用。

 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == 1 && resultCode == RESULT_OK && null != data) {
            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };

            Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);
            cursor.close();

            ImageView imageView = (ImageView) findViewById(R.id.imgView);
            imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

        }

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults)
{
   switch (requestCode) {
        case 1:
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
              Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
              startActivityForResult(galleryIntent, 1);
            } else {
                //Error message
            }
            break;
    }
}

答案 1 :(得分:0)

使用

$.get()

OR

$(function() {
  $.get("https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=BTC,USD,EUR,GBP", function(data) {
    alert(data)
    console.log(data)
  })
});

没有任何内容可以发布,可能需要<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

&#13;
&#13;
<textarea id="projectDescription" required [(ngModel)]="projectDescription" [formControl]="createNewForm.controls['projectDescription']" style="margin-bottom:-2%;width:50%;"></textarea>
&#13;
$("#projectDescription").val("Test value");
&#13;
&#13;
&#13;

答案 2 :(得分:0)

您的ready语法不正确。此外,在这种情况下,您可以使用$.get,尽管它可以正常工作。

<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script lang="javascript">$(document).ready(function () {
$.get("https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=BTC,USD,EUR,GBP", function(data) {
  alert(JSON.stringify(data))

})
}) </script>
</head>
<body>
</body>
</html>

答案 3 :(得分:0)

您想要的可能是$.get()而不是$.post()

虽然$.post(url, data, handler)通常用于POST(或$.get(url, handler)用于GET),但最好使用.done().fail()

$.post("https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=BTC,USD,EUR,GBP", {
    "field1" : "value1",
    "field2" : "value2"
}).done(function(data) {
    alert('Success: ' + data);
}).fail(function(xhr, status, error) {
    alert('Error: ' + error);
});

GET:

$.get("https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=BTC,USD,EUR,GBP").done(function(data) {
    alert('Success: ' + data);
}).fail(function(xhr, status, error) {
    alert('Error: ' + error);
});

另请注意,这应该在DOM准备就绪时完成(如果您需要将数据添加到某些HTML元素或类似的东西):

<script type="text/javascript">
    $(document).ready(function() {
        // Here
    });
</script>