最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

asp.net - How to call external webservice using jquery "jsonp"? - Stack Overflow

programmeradmin1浏览0评论

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

Share Improve this question edited May 23, 2017 at 11:52 CommunityBot 11 silver badge asked Apr 8, 2009 at 12:40 Amr ElgarhyAmr Elgarhy 69k70 gold badges192 silver badges310 bronze badges 2
  • Why have you accepted the answer to your other question if it didn't help you? You should keep asking in your old question and follow that thread. – Seb Commented Apr 8, 2009 at 12:48
  • because they answered me with this way: $.getJSON which is working, but i am asking about another way using $.ajax – Amr Elgarhy Commented Apr 8, 2009 at 12:52
Add a ment  | 

5 Answers 5

Reset to default 2

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

From memory:

  • Add [ScriptService] as an attribute to your Web Method
  • Also change your url to call the HelloWorld procedure.
    Something like http://localhost:1096/MySite/WebService.asmx/HelloWorld?callback

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

The point with JSONP is the P! P as in padding. You are padding the JSON object literal with a function call - invoking a function on the calling page taking the data object as an argument.

I.e. if you request the webservice, send the desired callback function name in the query string

...service/?callback=hello

Then the service should answer (using appropriate mime type):

hello({a: 17, b: 4117});

For a more in-depth explanation, see: http://www.stpe.se/2008/10/cross-site-data-retrieval-using-jsonp/

You can't issue a POST request using JSONP, only GET (because <script src="..."> GETs the resource).

Hezil's code worked for me, but I had to change the server code to this:

$data = '{"name" : "hello world"}'; echo $_GET['callback'] . '(' . $data . ');';

Note the "callback" instead of "jsoncallback".

First you should add jsonp ('callback') in your web server like $_GET['callback']

Second ,don't forget the ';' after the output scripts

$data = '{"name" : "hello world"}';
echo $_GET['jsoncallback'] . '(' . $data . ');';

Now you can find out why the "missing ; before statement" problem occured.

html:

$.getJSON({"http://localhost:1096/MySite/WebService.asmx?callback=?",
        function(data){alert(data);}
        });
发布评论

评论列表(0)

  1. 暂无评论