对 WCF Restful 服务的 Json 调用 (jquery) 始终运行错误函数
我正在尝试使用下面的 JavaScript 从不同的域调用我的 Web 服务(WCF Restful)(在下面的代码中,它们都在 localhost 中,但具有不同的端口号,我认为这是跨域请求)。
jQuery.support.cors = true;
$(document).ready(function () {
{
$.ajax({
type: "POST",
url: "http://localhost:52032/Service1.svc/std",
contentType: "application/jsonp; charset=utf-8",
dataType: "jsonp",
processData: false , //Same result if i remove this line
crossDomain : true , //Same result if i remove this line
converters: window.String, //Same result if i remove this line
success: OnSuccess,
error: OnError});
}
});
function OnSuccess(data)
{
alert("Finaly :D");
JSON.stringify(data);
}
function OnError(request, status, error)
{
alert("Error: "+request.statusText + JSON.stringify(request)+ " | " +JSON.stringify(status) +" | " +JSON.stringify(error));
}
我的网络服务非常简单。 getStudents()
返回一个 JSON 字符串。我使用 JavaScriptSerializer 将学生对象解析为 JSON。
namespace MyFirstWcfTestService
{
[ServiceContract]
public interface IService1
{
[OperationContract(Name = "AddParameter")]
[WebGet(UriTemplate = "/std", ResponseFormat = WebMessageFormat.Json)]
String getStudents();
}
}
我总是从 OnError
函数中收到以下错误,当我在 Chrome 开发/调试模式下查看响应时,我可以看到我的 JSON 文本。
错误: 成功{“readyState”:4,“status”:200,“statusText”:“成功”}|“ParserError”| “未调用 jQuery1706816276672761887_1322704382563”
我返回的 json 文本如下所示:
"[{\"学生ID\":256,\"学生名字\":\"阿里\",\"学生姓氏\":\"史密斯\"},{\"学生ID\":30 6、\"StudentFirstName\":\"John\",\"StudentSurname\":\"Menz\"},{\"StudentID\":314,\"StudentFirstName\" :\"George\",\"StudentSurname\":\"Ray\"},{\"StudentID\":316,\"StudentFirstName\":\"Fred\",\"StudentSu rname\":\"Patric\"},{\"StudentID\":603,\"StudentFirstName\":\"George\",\"StudentSurname\":\"Foster\"} ,{\"StudentID\":604,\"StudentFirstName\":\"Bobbie\",\"StudentSurname\":\"Kolman\"},{\"StudentID\":76 5、\"StudentFirstName\":\"Jim\",\"StudentSurname\":\"Khas\"},{\"StudentID\":987,\"StudentFirstName\":\ "Harry\",\"StudentSurname\":\"Poter\"},{\"StudentID\":988,\"StudentFirstName\":\"Con\",\"StudentSurn ame\":\"Mench\"},{\"StudentID\":1001,\"StudentFirstName\":\"Jim\",\"StudentSurname\":\"冒号\"}]"
您能帮我找到解决这个问题的方法吗?我已经将我的 Web 服务从 SOAP 更改为这个 Restful 服务。
编辑 我设法将我的 jsonp 消息包装在一个方法中(myCallback 如下
myCallback([{"StudentID":256,"StudentFirstName":"Rachel","StudentSurname":"Smith"},{"StudentID":306,"StudentFirstName":"Ali","StudentSurname":"弗莱明"},{"StudentID":314,"学生ntFirstName":"乔治","StudentSurname":"雷"},{"StudentID":316,"StudentFirstName":"弗雷德","StudentSurname":"帕特里克"},{"StudentID":603,"StudentFirstName" :"乔治","学生姓氏":"F oster"},{"StudentID":604,"StudentFirstName":"Bobbie","StudentSurname":"Kolman"},{"StudentID":765,"StudentFirstName":"Jim","StudentSurname":"Khas" },{"学生ID":987,"学生姓名e":"Harry","StudentSurname":"Poter"},{"StudentID":988,"StudentFirstName":"Con","StudentSurname":"Mench"},{"StudentID":1001,"StudentFirstName" :"Jim","StudentSurname":"冒号"}]);
我还更改了我的 Ajax 调用,如下所示
$.ajax({
type: "post",
url: "http://localhost:52032/Service1.svc/getStd?callback=myCallback", //or callback=?
dataType: "jsonp",
//converters: jQuery.parseJSON,
contentType: 'application/javascript',
success: OnSuccess,
error: OnError
});
但我仍然遇到相同的错误。我为这样一个简单的任务花了三周的时间:(
I am trying to call my web service (WCF Restful) from a different domain (in the following code they are both in localhost but with different port number which I suppose considered as cross-domain request) using the JavaScript below.
jQuery.support.cors = true;
$(document).ready(function () {
{
$.ajax({
type: "POST",
url: "http://localhost:52032/Service1.svc/std",
contentType: "application/jsonp; charset=utf-8",
dataType: "jsonp",
processData: false , //Same result if i remove this line
crossDomain : true , //Same result if i remove this line
converters: window.String, //Same result if i remove this line
success: OnSuccess,
error: OnError});
}
});
function OnSuccess(data)
{
alert("Finaly :D");
JSON.stringify(data);
}
function OnError(request, status, error)
{
alert("Error: "+request.statusText + JSON.stringify(request)+ " | " +JSON.stringify(status) +" | " +JSON.stringify(error));
}
My web service is pretty simple. getStudents()
returns a JSON string. I used JavaScriptSerializer
to parse my student object to JSON.
namespace MyFirstWcfTestService
{
[ServiceContract]
public interface IService1
{
[OperationContract(Name = "AddParameter")]
[WebGet(UriTemplate = "/std", ResponseFormat = WebMessageFormat.Json)]
String getStudents();
}
}
I always get the following error from OnError
function, and when I look at the response in Chrome development/debug mode I can see my JSON text.
Error:
success{"readyState":4,"status":200,"statusText":"success"}|"ParserError"|
"jQuery1706816276672761887_1322704382563 was not called"
My returned json text looks like this :
"[{\"StudentID\":256,\"StudentFirstName\":\"Ali\",\"StudentSurname\":\"Smith\"},{\"StudentID\":306,\"StudentFirstName\":\"John\",\"StudentSurname\":\"Menz\"},{\"StudentID\":314,\"StudentFirstName\":\"George\",\"StudentSurname\":\"Ray\"},{\"StudentID\":316,\"StudentFirstName\":\"Fred\",\"StudentSurname\":\"Patric\"},{\"StudentID\":603,\"StudentFirstName\":\"George\",\"StudentSurname\":\"Foster\"},{\"StudentID\":604,\"StudentFirstName\":\"Bobbie\",\"StudentSurname\":\"Kolman\"},{\"StudentID\":765,\"StudentFirstName\":\"Jim\",\"StudentSurname\":\"Khas\"},{\"StudentID\":987,\"StudentFirstName\":\"Harry\",\"StudentSurname\":\"Poter\"},{\"StudentID\":988,\"StudentFirstName\":\"Con\",\"StudentSurname\":\"Mench\"},{\"StudentID\":1001,\"StudentFirstName\":\"Jim\",\"StudentSurname\":\"Colon\"}]"
Would you please help me to find a solution for this problem. I have already changed my web service from a SOAP to this restful one.
EDIT
I managed to get my jsonp message wraped in an method (myCallback as follow
myCallback([{"StudentID":256,"StudentFirstName":"Rachel","StudentSurname":"Smith"},{"StudentID":306,"StudentFirstName":"Ali","StudentSurname":"Flemming"},{"StudentID":314,"StudentFirstName":"George","StudentSurname":"Ray"},{"StudentID":316,"StudentFirstName":"Fred","StudentSurname":"Patric"},{"StudentID":603,"StudentFirstName":"George","StudentSurname":"Foster"},{"StudentID":604,"StudentFirstName":"Bobbie","StudentSurname":"Kolman"},{"StudentID":765,"StudentFirstName":"Jim","StudentSurname":"Khas"},{"StudentID":987,"StudentFirstName":"Harry","StudentSurname":"Poter"},{"StudentID":988,"StudentFirstName":"Con","StudentSurname":"Mench"},{"StudentID":1001,"StudentFirstName":"Jim","StudentSurname":"colon"}]);
And I also change my Ajax call as follow
$.ajax({
type: "post",
url: "http://localhost:52032/Service1.svc/getStd?callback=myCallback", //or callback=?
dataType: "jsonp",
//converters: jQuery.parseJSON,
contentType: 'application/javascript',
success: OnSuccess,
error: OnError
});
But I still get the same error. I'm straggling for 3 weeks for such a simple task :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试更改 getStudents() 以返回 Json(List, JsonRequestBehavior.AllowGet) 并通过 jQuery 访问返回的列表,每个使用 data[iterator].StudentID、data[iterator].StudentFirstName 等。
Try changing getStudents() to return Json(List, JsonRequestBehavior.AllowGet) and accessing the returned list through a jQuery each using data[iterator].StudentID, data[iterator].StudentFirstName, etc.
尝试以下更改:
在客户端:
Try this changes:
On the client-side: