2

我正在尝试使用 ajax 调用从 web api 带回数据。我写了 2 个类似的函数,但都不起作用。我可以看到数据通过 Fiddler 返回,但对于以下两个函数,它不会进入成功调用。我究竟做错了什么?数据在 Fiddler 的两个函数中返回,只是没有成功。

这是尝试1:

    function PopulateDivisions()
{
    $.support.cors=true;


    $.ajax({
        type:'GET',
        url:'http://IP/Service/api/DivisionSearch/GetAllDivisions',
        data: {},
        contentType: 'application/json; charset=utf-8',
        dataType: 'jsonp',

        success: function(data) {

                alert(data);

                $("#divisionSelect").append($('<option></option>').val("-99").html("Select One"));
            $.each(data, function(i, item){
                $("#divisionSelect").append($('<option></option>').val(item.Name).html(item.Name));
            });
        },
        error: function(xhrequest, ErrorText, thrownError) {
            alert("Original: " + thrownError + " : " + ErrorText);
        }
    });

}

错误:未调用 jQuery19102671239298189216_1382022403977:解析器错误

这是 Fiddler 显示的数据返回:

    [{"Id":1,"Description":"Executive","Name":"Executive "},{"Id":2,"Description":"ASD","Name":"Administrative Services Division "},{"Id":3,"Description":"COM","Name":"Communications "},{"Id":4,"Description":"CP","Name":"Contracts and Procurement "},{"Id":5,"Description":"PMD","Name":"Program Management Division "},{"Id":6,"Description":"RED","Name":"Research and Evaluation Division "},{"Id":7,"Description":"IT","Name":"Information Technology "}]

这是尝试#2:

    function PopulateDivisions2()
{


    $.support.cors=true;


    $.ajax({
        type:'GET',
        url:'http://IP/Service/api/DivisionSearch/GetAllDivisionsJsonP',
        data: {},
        contentType: 'application/json; charset=utf-8',
        dataType: 'jsonp',
        jsonp: false,
        jsonpCallback: "myJsonMethod",

        success: function(data) {
                //data = JSON.parse(data):
                alert(data);

                $("#divisionSelect").append($('<option></option>').val("-99").html("Select One"));
            $.each(data, function(i, item){
                $("#divisionSelect").append($('<option></option>').val(item.Name).html(item.Name));
            });
        },
        error: function(xhrequest, ErrorText, thrownError) {
            alert("PopulateDivisions2:  " + thrownError + " : " + ErrorText);
        }

    });

}

错误:未调用 myJsonMethod:parsererror

这是 Fiddler 显示的数据回来了:

    "myJsonMethod([{\"Id\":1,\"Description\":\"Executive\",\"Name\":\"Executive \"},{\"Id\":2,\"Description\":\"ASD\",\"Name\":\"Administrative Services Division \"},{\"Id\":3,\"Description\":\"COM\",\"Name\":\"Communications \"},{\"Id\":4,\"Description\":\"CP\",\"Name\":\"Contracts and Procurement \"},{\"Id\":5,\"Description\":\"PMD\",\"Name\":\"Program Management Division \"},{\"Id\":6,\"Description\":\"RED\",\"Name\":\"Research and Evaluation Division \"},{\"Id\":7,\"Description\":\"IT\",\"Name\":\"Information Technology \"}]);"
4

2 回答 2

2

contentType: 'application/json; charset=utf-8'告诉您的服务器您正在发送 JSON 数据,但您没有任何要发送的数据。试着离开那个设置。

如果您要在浏览器中浏览您url的浏览器,您会返回 json 吗?

我不确定这是否重要,但我会删除该error设置,因为它在jQuery Ajax 文档中说This handler is not called for cross-domain script and cross-domain JSONP requests.

我会尝试用最少的配置来运行它,如下所示:

$.ajax({
    url:'http://IP/Service/api/DivisionSearch/GetAllDivisions',
    dataType: 'jsonp',
    success: function(data) { console.log(data); }
});

看看这是否有效,然后在它之上构建。没有 jsfiddle 很难调试正在发生的事情。

这是一个对您来说应该是一个很好的资源的链接:Basic example of using .ajax() with JSONP?

于 2013-10-17T15:16:22.920 回答
0
 function PopulateDivisions2(){
$.support.cors=true;
$.ajax({
    type:'GET',
    url:'http://IP/Service/api/DivisionSearch/GetAllDivisionsJsonP?callback=?',
    data: {},
    contentType: 'application/json; charset=utf-8',
    dataType: 'jsonp',
    jsonpCallback: "myJsonMethod" });


function myJsonMethod(data) {
            //data = JSON.parse(data):
            alert(data);$("#divisionSelect").append($('<option></option>').val("-99").html("Select One"));
        $.each(data, function(i, item){
            $("#divisionSelect").append($('<option></option>').val(item.Name).html(item.Name));
        });
    }}

你可以试试上面的代码吗?我已删除成功回调并在查询字符串中包含回调。

于 2013-10-19T13:06:27.367 回答