作者:新梅乀_Esther | 来源:互联网 | 2024-11-07 09:24
本文探讨了如何利用jQuery的JSONP技术实现跨域调用外部Web服务。通过详细解析JSONP的工作原理及其在jQuery中的应用,本文提供了实用的代码示例和最佳实践,帮助开发者解决跨域请求中的常见问题。
I had a previous question can jquery ajax call external webservice?
我有一个先前的问题可以jquery ajax调用外部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:
一些优秀的开发人员回答我使用jsonp,但我不知道如何使用它,我试图使用此代码调用我的服务:
$.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} )"
但它总是在返回时给我这个错误:“丢失;在声明之前[打破此错误]({”x“:10,”y“:100})”
and never call the success function, can anyone help with that?
并且从不调用成功函数,任何人都可以帮忙吗?
5 个解决方案