如何使用jQuery&QUOT调用外部Web服务; JSONP"? [英] How to call external webservice using jquery "jsonp"?

查看:135
本文介绍了如何使用jQuery&QUOT调用外部Web服务; JSONP"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个previous问题可以jQuery的AJAX调用外部web服务?


和一些好的开发商回答我使用JSONP,但我不知道如何使用它,我试图用这个code打电话给我的服务:

  $。阿贾克斯({
            键入:POST,
            网址:HTTP://本地主机:1096 / mysite的/ WebService.asmx回调=?,
            数据:{},
            的contentType:应用/ JSON的;字符集= UTF-8,
            数据类型:JSONP
            成功:函数(MSG){警报(味精);}
            });



这是我的服务,code:

  [的WebMethod]
公共字符串的HelloWorld(){
    返回的Hello World;
}


任何人有例子或可以解释这个问题给我吗?

更新

我又写了code是这样的:

<$p$p><$c$c>$.getJSON(\"http://localhost:1096/YourShoppingTest1/WebService.asmx/HelloWorld?jsonp=?\",{name:\"test\"},
    功能(数据){
    警报(data.x);
    });


像这样的服务:

  [的WebMethod]
公共字符串的HelloWorld(字符串名称)
{
    回归({\\X \\:10,\\Y \\:100});
}


但它总是给我这个错误,当回:失踪;语句之前
[关于此错误BREAK]({X:10,Y:100})

和永远不会调用成功功能,任何人都可以提供帮助的?


解决方案

我有一个类似的问题,遗憾的是我没有在手code。

从记忆:

请参阅:<一href=\"http://stackoverflow.com/questions/448879/what-are-some-good-examples-of-jquery-using-jsonp-talking-to-net\">What使用JSONP交谈.NET 和放大器的jQuery一些很好的例子?; <一href=\"http://stackoverflow.com/questions/426420/what-is-the-best-way-to-call-a-net-webservice-using-jquery\">What是使用jquery调用webservice的.NET的最佳方式?

I had a previous question can jquery ajax call external webservice?


and some good developers answered me to use jsonp, but i don't know how to use it, i am trying to call my service using this code:

$.ajax({
            type: "POST",
            url: "http://localhost:1096/MySite/WebService.asmx?callback=?",
            data: "{}",
            contentType: "application/json; charset=utf-8",
            dataType: "jsonp",
            success: function(msg) {alert(msg);}
            });


and this is my service code:

[WebMethod]
public string HelloWorld() {
    return "Hello World " ;
}


anyone have examples or can explain this issue for me?

UPDATE:
I wrote the code again to be like this:

$.getJSON("http://localhost:1096/YourShoppingTest1/WebService.asmx/HelloWorld?jsonp=?",{name:"test"},
    function(data){
    alert(data.x);
    });


and the service like this:

[WebMethod]
public string HelloWorld(string name)
{
    return "( {\"x\":10 , \"y\":100} )";
}


But it always give me this error when back: "missing ; before statement [Break on this error] ( {"x":10 , "y":100} )"

and never call the success function, can anyone help with that?

解决方案

I've had a similiar problem, unfortunately I don't have the code at hand.

From memory:

See: What are some good examples of JQuery using JSONP talking to .net? & What is the best way to call a .net webservice using jquery?

这篇关于如何使用jQuery&QUOT调用外部Web服务; JSONP&QUOT;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆