JSONP 响应格式错误

Posted

技术标签:

【中文标题】JSONP 响应格式错误【英文标题】:JSONP response in the wrong format 【发布时间】:2013-07-31 02:27:32 【问题描述】:

我正在构建一个 JSONP 服务。

如果我使用System.ServiceModel.Activation.WebScriptServiceHostFactory,它会起作用。

我需要使用System.ServiceModel.Activation.WebServiceHostFactory,因为我想使用 URITemplate 以便可以传递参数。当我切换到这个工厂时,它不再将响应编码为 jsonp。所以我从我的 javascript 中收到一个错误,无法弄清楚如何处理 "isbn": "~1234567890~"

这是我需要序列化的地方吗?如何将其添加到此 c# 代码中:

using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.IO;
using System.Text;
using System;
using System.Collections;
using System.Collections.Generic;
using mysql.Data.MySqlClient;
using System.Runtime.Serialization.Json;



namespace Microsoft.Samples.Jsonp


    [DataContract]
    public class Response
    
        [DataMember]
        public string isbn;

    




    [ServiceContract(Namespace = "JsonpAjaxService")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class CustomerService
    
        [WebGet(ResponseFormat = WebMessageFormat.Json)]
        public Response GetCustomer()
        

            string isbns = "";
            /*string line= "";*/
            try
            
                MySqlConnection sqlConnection1 = new MySqlConnection("Server=localhost;  Database=mydb; Uid=peggy; Pwd=dpL'engl3");

                MySqlCommand cmd = new MySqlCommand();
                MySqlDataReader reader;

                cmd.CommandText = "SELECT name, obitdate, page FROM dobits";
                /* cmd.CommandType = CommandType.Text;*/
                cmd.Connection = sqlConnection1; 
                sqlConnection1.Open();
                reader = cmd.ExecuteReader();
                // Data is accessible through the DataReader object here.

                while (reader.Read())
                
                    isbns = isbns + '~' + reader.GetString(0) + '^' + reader.GetString(1) + '#' + reader.GetString(2);
                
                sqlConnection1.Close();

            
            catch (Exception e)
            
                System.IO.Directory.CreateDirectory(@"c:\data\exception");
                Console.WriteLine("The file could not be read");
                Console.WriteLine(e.Message);
            
            isbns = isbns + "~";
            System.IO.Directory.CreateDirectory(@"c:\data\ReadytoReturn");
            return new Response()  isbn= isbns ;
        

    

html 如下所示:

<script type="text/javascript">

$(function() 

 $.getJSON( 'http://192.168.64.180/dobits/service.svc/GetCustomer?callback=?', null,  function(res)


//console.log(res);              // log the result from the callback
//var parsed = jQuery.parseJSON(res);
 $.each(res, function(key, val) 

var first = val.indexOf("~");
var next = val.indexOf("~",first+1);

while (next>=0)

     $("#homeJacket").append('<p>'+val.substring(first+1,next)+'</p>');

     first=next;
     next=val.indexOf("~",first+1);



);

);


);


</script>


<div id="homeJacket">
    <p></p>

</div>

我从 firebug 得到的错误信息是:

SyntaxError: invalid label
"isbn":"~1234567890~"

【问题讨论】:

【参考方案1】:

我能够使用此处描述的技术将其转换为 JSONP:

https://sites.google.com/site/dhtmlexperiments/blogs/crossdomainrestfulserviceinwcf

【讨论】:

以上是关于JSONP 响应格式错误的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 JSON.NET 使用十六进制编码字符解析格式错误的 JSONP?

JSONP 响应被截断

Express响应方法

如何在 node.js + Express 中同时支持 json 和 jsonp 响应?

php文件jsonp返回字符串格式

json和jsonp