2012-12-27 117 views
3

我是新來的web服務。在我的項目中,當我試圖運行時,我連接了Web Service(一切都已準備就緒),我得到了下面的錯誤。未捕獲的SyntaxError:意外的令牌< - 在jQuery中ajax

錯誤 - >

Uncaught SyntaxError: Unexpected token <

的Web服務和我的網頁是在同一個解決方案,但不同的項目。

相關的代碼如下:

jQuery的(網址:11761)

function GetAllCategories() { 
    $.ajax({ 
     url: "http://localhost:12015/myWebService.asmx?op=GetCategories", 
     type: "POST", 
     dataType: "jsonp", 
     data: "{}", 
     contentType: "application/jsonp; charset=utf-8", 
     success: function (data) { 
      var categories = data.d; 
      $.each(categories, function (index, category) { 
       alert(category.CategoryId); 
      }); 
     }, 
     error: function (e) { 
      alert(e.message); 
     } 
    }); 
} 

Web服務(網址:12015)

[WebMethod] 
[ScriptMethod(ResponseFormat = ResponseFormat.Json)] 
public List<Categories> GetCategories() 
{ 
    //Code 
} 

之前問我在這裏已經通過這link(不能理解它)

編輯:

得到備選答案從這個post

+0

確保您最後沒有'<'標籤。 – Blender

+0

@Blender不存在..沒有代碼錯誤的權利? –

+0

何時發生此錯誤(在您的AJAX調用或腳本加載後)? – Blender

回答

5

通過您的網站和Web服務在兩個不同的項目中運行,可以發現您打破了Same origin policy

Webservice和網站移動到同一個項目它應該工作。

而且JavaScript是錯了,它應該是

function GetAllCategories() { 
    $.ajax({ 
     url: "http://localhost:12015/myWebService.asmx/GetCategories", 
     type: "POST", 
     dataType: "jsonp", 
     data: "{}", 
     contentType: "application/jsonp; charset=utf-8", 
     success: function (data) { 
      var categories = data.d; 
      $.each(categories, function (index, category) { 
       alert(category.CategoryId); 
      }); 
     }, 
     error: function (e) { 
      alert(e.message); 
     } 
    }); 
} 

運算位只在那裏在Web瀏覽器測試。刪除它,你應該很好。

PS @andyb公平地提出了這個答案,但不清楚這是問題所在! UPDATE

在今天做了一些關於jiggery pokery,我已經澄清了幾點,我想我會分享。你必須POST.asmx服務,你不能做這個跨域。你可以在域之間觸發GET,但不會觸發POST,所以這是我相信的路由問題。您可以啓用GET,請參閱How to call an ASMX web service via GET?。但是,這似乎是一個壞主意,因爲它會暴露你的webservice所有和各式各樣!

+0

+1抱歉,我無法參與聊天,但這似乎與上述問題中的註釋一致:-) – andyb

相關問題