使用JQuery使用Java Web服务:错误?

时间:2012-08-10 10:35:10

标签: java jquery web-services

我在Netbeans 7.1.2中创建了一个Java Web服务,并设法通过netbeans创建一个Java客户端来测试它,并且它有效。然后我尝试创建一个jQuery客户端但失败了。

jQuery客户端代码是:

$.ajax({
    type: "POST",
    url: "http://luke-test.j.layershift.co.uk/ClubInService/getHello",
    data: "{val:luke}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: OnSuccessCall,
    error: OnErrorCall
});

function OnSuccessCall(response) {
    $tmp = "";
    $.each(response, function(index, element) {
        $tmp += element + ";";
    });
    alert($tmp);
}


function OnErrorCall(response) {
    $tmp = "";
    $.each(response, function(index, element) {
        $tmp += element + ";";
    });
    alert($tmp);
}

我试图呼叫的功能是:

@WebMethod(operationName = "getHello")
public String getHello(@WebParam(name = "val", targetNamespace =
     "http://clubinservice/") String val) {
   return "Hello " + val + "!";
}

当使用jQuery来使用Java Web服务时,我收到此错误并且不知道它意味着什么:

  

0;函数(a,b){if(!s){var c = a.toLowerCase(); a = m [c] = m [c] || a,l [a] = b} return这个}; function(){return s === 2?n:null}; function(a){var c; if(s === 2){if(!o){o = {}; while(c = bG.exec(n))o [c [1] .toLowerCase()] = c [2]} c = o [a.toLowerCase()]}返回c === b?null:c}; function( a){s ||(d.mimeType = a); return this}; function(a){a = a ||“abort”,p&amp;&amp; p.abort(a),w(0,a);返回此}}; function(){if(c){var a = c.length; n(arguments),j?l = c.length:e&amp; e!==!0&amp;&amp;(k = a ,o(e [0],e [1])}}返​​回此}}; function(){if(c){var a = c.length; n(arguments),j?l = c.length:e&amp; &amp; e!==!0&amp;&amp;(k = a,o(e [0],e [1])}}返​​回此}}; function(){if(c){var a = c.length; n(自变量),j?l = c.length:e&amp;&amp; e!==!0&amp;&amp;(k = a,o(e [0],e [1]))}返回此};函数(){return e}; function(){return !! i}; function(){return !! i}; function(a,b,c){i.done(a).fail(b).progress( c); return this}; function(){i.done.apply(i,arguments).fail.apply(i,arguments); return this}; function(a,b,c){return f.Deferred(function) (d){f.each({完成:[一个, “解析”],失败:并[b,“重新ject“],进度:[c,”notify“]},函数(a,b){var c = b [0],e = b [1],g; f.isFunction(c)?ia:ia} }})。promise()}; function(a){if(a == null)a = h; else for(var b in h)a [b] = h [b]; return a}; function() {if(c){var a = c.length; n(arguments),j?l = c.length:e&amp;&amp; e!==!0&amp;&amp;(k = a,o(e [0] ,e [1]))}返回这个}; function(){if(c){var a = c.length; n(arguments),j?l = c.length:e&amp;&amp; e!==! 0&amp;&amp;(k = a,o(e [0],e [1])}}返​​回此}}; function(){if(c){var a = c.length; n(arguments),j? l = c.length:e&amp;&amp; e!==!0&amp;&amp;(k = a,o(e [0],e [1]))} return this}; function(a){if(a ){var b; if(s <2)for(b in a)j [b] = [j [b],a [b]];否则b = a [v.status],v.then(b, b)}返回此} ;; 0;错误;

如果有帮助,请随意尝试使用Web服务上的功能。任何帮助都会很棒!

由于

修改

在意识到我应该使用JSON.stringify之后:

function OnSuccessCall(response) {
    alert(JSON.stringify(response));
}


function OnErrorCall(response) {
    alert(JSON.stringify(response));
}

我收到了不同的错误消息:

{"readyState":0,"responseText":"","status":0,"statusText":"error"}

在查看herehere后,我尝试了

$.post("http://luke-test.j.layershift.co.uk/ClubInService/getHello",
{val: "John"},
function(data){
    alert(JSON.stringify(data));
}, "json")
.error(function(data){alert(JSON.stringify(data));});

还返回了错误消息:

{"readyState":0,"responseText":"","status":0,"statusText":"error"}

1 个答案:

答案 0 :(得分:1)

  • 尝试相对网址
  • 使用:contentType:“application / json”,
  • add:dataType:“json”,

另外,如果您访问的另一个网址不是为您的内容提供服务的网址,则浏览器会提供安全保护。在这种情况下,您应该阅读jsonp:

http://www.fbloggs.com/2010/07/09/how-to-access-cross-domain-data-with-ajax-using-jsonp-jquery-and-php/