JSONP JSON回调方法不叫:parsererror [英] JSONP json callback method not called : parsererror

查看:3510
本文介绍了JSONP JSON回调方法不叫:parsererror的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下AJAX功能使用JSONP:

I have the following ajax function using jsonp:

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


    $.ajax({
        type:'GET',
        url:'http://IP/Service/api/DivisionSearch/GetAllDivisions?callback=?',
        dataType: "jsonp",
        //jsonp: false,
        jsonpCallback: "myJsonMethod",

        success: function(data) {

                alert('yes');

                $("#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);
        }
    });


}

我收到以下错误:         myJsonMethod不叫:parsererror

I am getting the following error: myJsonMethod was not called : parsererror

如果我看的提琴手,我得到了以下数据传回,我增加了回调的名字前面,因为我看到了建议,如果我把它拿出来它仍然无法正常工作。

If I look at Fiddler, I am getting the following data back, I added the callback name to the front, as I saw that suggested, if I take it out it still doesn't work.

    "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 \"}])"

下面是我的控制器的方法:

Here is the method in my controller:

     public string GetAllDivisions(string callback)
    {
        var divisions = _DivisionModel.GetAllDivisions();

        var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();

        string json = serializer.Serialize(divisions);

        string result = callback + "(" + json + ");";

        return result; 
    }

我没有收到我的成功电话,我丢失或做错了吗?

I'm not getting to my success call, what am I missing or doing wrong?

推荐答案

您不必须指定一个成功的回调,因为JSONP回调会自动发生,因为在服务器端code将在案件JSONP的返回的JavaScript。

You dont have to specify a success callback because the jsonp callback will happen automatically as the server side code will return javascript in case of jsonp.

请参阅下面的答案工作的例子。

Refer the below answer for working example.

<一个href="http://stackoverflow.com/questions/19351934/using-jquery-jsonp-returns-error-callback-function-was-not-called/19423625#19423625">Using jQuery的JSONP返回错误回调函数没有被称为 <一href="http://stackoverflow.com/questions/19351934/using-jquery-jsonp-returns-error-callback-function-was-not-called/19423625#19423625">

这篇关于JSONP JSON回调方法不叫:parsererror的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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