Jquery Ajax /GET 方法成功时什么也没发生

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11069986/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 10:09:39  来源:igfitidea点击:

Jquery Ajax /GET method nothing happend when success

jqueryhttpget

提问by UsTa

I have a RESTful web service which runs on Glassfish Application Server. When I invoke the web services with /GET HTTP method on cURL, stored entries are fetched to console. I want to make a jQuery REST client - when I click the button, it must alert me to the returned JSON or XML entries. But in success method, nothing happened. My html page looks like below.

我有一个运行在 Glassfish 应用服务器上的 RESTful Web 服务。当我在 cURL 上使用 /GET HTTP 方法调用 Web 服务时,存储的条目被提取到控制台。我想制作一个 jQuery REST 客户端 - 当我单击按钮时,它必须提醒我返回的 JSON 或 XML 条目。但在成功方法中,什么也没发生。我的 html 页面如下所示。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Ba?l?ks?z Belge</title>

</head>

<body>
<input type="submit" name="kaydet" id="kaydet" value="Kaydet" />
<script type="text/javascript" src="jquery-1.7.2.js"></script>
<script type="text/javascript">
var restURL="http://localhost:43842/KodcuComRESTful/kodcuRS/yazilar";

$('#kaydet').click(function(){
      $.ajax({
    type: 'GET',
    url: restURL,
    dataType:"json",
    success: renderList,
      });
      return false;
});


function renderList(data) {
    alert(data);
}
</script>
</body>
</html>

When I observed the request and response in Live HTTP headers program, it seems everything is OK. What is the problem? enter image description here

当我观察 Live HTTP headers 程序中的请求和响应时,似乎一切正常。问题是什么? 在此处输入图片说明

回答by vijay

I have implemented something you want to. Here is the code. And it works completely fine.

我已经实现了你想要的东西。这是代码。它工作得很好。

Hope it helps you.

希望对你有帮助。

 <html>
 <script type="text/javascript" src="jQuery.js"></script>
  <script type="text/javascript">
    $(document).ready(function(){
        $("#submit").click(function(){
        $.ajax({
            type: 'GET',
            url: "http://localhost/page1.html",
            success:function(data){
             alert(data);
            }
        });
    return false;
    });
});
</script>
</head>

<body>
<input type="button" id="submit" value="submit" />
</body>
 </html>

回答by Pol

Two things to check:

要检查的两件事:

  1. Comment out dataType:"json",and see if it is working. Maybe this is not JSON.
  2. Is this HTML on the same domain as restURLpoints to? If not, you may need JSONP.
  1. 注释掉dataType:"json",,看看它是否有效。也许这不是 JSON。
  2. 此 HTML 与restURL指向的域位于同一域中吗?如果没有,您可能需要JSONP

回答by Max

There is a comma - "," after success function line, remove it as it is the last line?

成功功能行后有一个逗号 - “,”,将其删除,因为它是最后一行?

Currently it is:

目前是:

success: renderList,

成功:renderList,

change it to: success: renderList

将其更改为:成功:renderList

Final Html:

最终HTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Ba?l?ks?z Belge</title>

</head>

<body>
<input type="submit" name="kaydet" id="kaydet" value="Kaydet" />
<script type="text/javascript" src="jquery-1.7.2.js"></script>
<script type="text/javascript">
var restURL="http://localhost:43842/KodcuComRESTful/kodcuRS/yazilar";

$('#kaydet').click(function(){
      $.ajax({
    type: 'GET',
    url: restURL,
    dataType:"json",
    success: renderList
      });
      return false;
});


function renderList(data) {
    alert(data);
}
</script>
</body>
</html>

回答by Rahul Bhaskar

You can also use this code

您也可以使用此代码

<script>
$("#submit").click(function(){

var val = $("#text").val();

$.get('get.php?id='+val,function(data,status){

if(status == "success")
{
  $("#show").val(data);
}
});

});
</script>