Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过JQuery发布的Web服务不起作用_Jquery - Fatal编程技术网

通过JQuery发布的Web服务不起作用

通过JQuery发布的Web服务不起作用,jquery,Jquery,我有一个RESTfulWebService,我正试图通过我的网页上的jquery调用发出一个HTTPPOST请求 <html> <head> <script type="text/javascript" src="js/newjavascript.js"></script> <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jqueryui.cs

我有一个RESTfulWebService,我正试图通过我的网页上的jquery调用发出一个HTTPPOST请求

<html>
<head>
<script type="text/javascript" src="js/newjavascript.js"></script>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jqueryui.css" rel="stylesheet" type="text/css"/>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js">       </script>
<script type="text/javascript" src="js/jquery-ui-1.8.22.custom.min.js"> </script>
<script type="text/javascript" src="js/jquery-1.7.2.min.js"> </script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>

<input type="text" name="name" value="" id="name"/>
<input type="submit" value="submit" onclick="intialize()"/>
<script>
function formToJSON() {
  return JSON.stringify({"name": $('#name').val() });
 }
  </script>

  <script>
   function intialize(){
   $.ajax({
   type: 'POST',
   contentType: 'application/json',
   url: "http://localhost:41191/test/resources/storeincompleteform",
   dataType: "json",
   data: formToJSON(),
   success: function(data, textStatus, jqXHR){
        alert(' success');

    },
    error: function(jqXHR, textStatus, errorThrown){
        alert('error: ' + textStatus);
    }
  });

      }
  </script>
  </body>
  </html> 

函数formToJSON(){
返回JSON.stringify({“name”:$('#name').val());
}
函数初始化(){
$.ajax({
键入:“POST”,
contentType:'应用程序/json',
url:“http://localhost:41191/test/resources/storeincompleteform",
数据类型:“json”,
数据:formToJSON(),
成功:函数(数据、文本状态、jqXHR){
警惕(‘成功’);
},
错误:函数(jqXHR、textStatus、errorshown){
警报('错误:'+文本状态);
}
});
}

但是上面的代码不起作用。Web服务工作正常

您的脚本正在发布到其他端口。这与浏览器的功能冲突

如果您试图从服务器检索数据,您可以将技术与一些技术结合使用来规避此问题

但是,由于您试图将数据发布到服务器,因此有两种选择:

  • 确保XMLHttpRequest的来源相同
  • 利用现代浏览器中可用的技术;在这种情况下,您必须将服务器配置为提供
    Access Control Allow Origin
    标题,以允许您发布的站点

  • 这是一个XSS问题,可能是由于不同的端口造成的。更改:

    dataType: "json"
    
    致:


    此警报还会显示一条错误消息。。为什么要导入jQuery两次?1.5和1.7?。。并且总是在依赖.js文件之前加载jQuery。你说不工作是什么意思?它会给你一个错误,还是什么都没有发生?@arpitolanki错误是怎么说的?我猜你从来没有听说过
    dataType: "jsonp"