2012-09-27 65 views
0

我有一個寧靜的web服務,我試圖通過我的網頁通過jquery調用HTTP POST請求。通過JQuery Webservice POST不起作用

<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> 

但上面的代碼是不working.The web服務工作正常

+0

任何錯誤? –

+0

該警報顯示錯誤消息 –

+0

另外..你爲什麼要兩次導入jQuery? 1.5和1.7?..總是在依賴.js文件之前加載jQuery –

回答

0

你的腳本被髮布到不同的端口。這與瀏覽器的Same Origin Policy衝突。

如果你想從服務器獲取數據,可以結合使用jQuery'sbuilt-inJSONP技術與一些server-side scripting規避這一點。

然而,因爲在你的情況你想數據到服務器時,你有兩個選擇:

  1. 確保的XMLHttpRequest是同一淵源考
  2. 利用的cross-domain XMLHttpRequest現代瀏覽器中可用的技術;在這種情況下,你必須配置你的服務器來服務Access-Control-Allow-Origin標題,允許你發佈的網站。
0

這是一個XSS問題,可能是由於不同的端口。更改:

dataType: "json" 

到:在控制檯

dataType: "jsonp"