croix domaine d'appel avec jQuery jsonp à ASP.NET service web

Mon problème est un problème connu et discuté ici et ici.
Mais même après avoir lu et de mettre en œuvre les solutions proposées, je suis incapable de faire ce travail.

Le problème: le service web retournant xml insted json:

<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">"Now i am getting jsop string""2nd param"</string>

Maintenant permet de diviser le code en sections:

LE SERVEUR DISTANT (IIS 7.0 .NET 4):


web.config:

<?xml version="1.0"?>
<configuration>
        <system.webServer>
            <modules>
                <add name="JsonHttpModule.JsonHttpModule" type="JsonHttpModule"/>
            </modules>
        </system.webServer>
    <system.web.extensions>
        <scripting>
            <webServices>
                <jsonSerialization maxJsonLength="102400"/>
            </webServices>
        </scripting>
    </system.web.extensions>
    <system.web>
        <compilation debug="true" targetFramework="4.0" />
        <customErrors mode="Off"/>
        <webServices>
            <protocols>
                <add name="HttpGet"/>
                <add name="HttpPost"/>
            </protocols>
        </webServices>
    </system.web>
</configuration>


le service web:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
using JsonHttpModule;
///<summary>
///Summary description for JSONP_EndPoint
///</summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
[System.Web.Script.Services.ScriptService]
public class MyService : System.Web.Services.WebService {
    [WebMethod]
    [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
    public string Sum(string x, string y)
    {
        return x + y;
    }

}

la classe HttpModule:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Text;
///<summary>
///Summary description for ContentTypeHttpModule
///</summary>
namespace JsonHttpModule
{
public class JsonHttpModule : IHttpModule
{
private const string JSON_CONTENT_TYPE = "application/json; charset=utf-8";
public void Dispose()
{
}
public void Init(HttpApplication app)
{
app.BeginRequest += OnBeginRequest;
app.EndRequest += new EventHandler(OnEndRequest);
}
public void OnBeginRequest(object sender, EventArgs e)
{
HttpApplication app = (HttpApplication)sender;
HttpRequest request = app.Request;
//Make sure we only apply to our Web Service
if (request.Url.AbsolutePath.ToLower().Contains("MyService.asmx"))
{
if (string.IsNullOrEmpty(app.Context.Request.ContentType))
{
app.Context.Request.ContentType = JSON_CONTENT_TYPE;
}
app.Context.Response.Write(app.Context.Request.Params["callback"] + "(");
}
}
void OnEndRequest(object sender, EventArgs e)
{
HttpApplication app = (HttpApplication)sender;
HttpRequest request = app.Request;
if (request.Url.AbsolutePath.ToLower().Contains("MyService.asmx"))
{
app.Context.Response.Write(")");
}
}
}
}

CÔTÉ CLIENT (localhost):

<script>
$(function () {
$('#btn_test').click(function () {
$.ajax({ url: "http://tonofweb.com/MyService.asmx/Sum",
data: { x: JSON.stringify("Now i am getting jsop string"), y: JSON.stringify("2nd param") },
dataType: "jsonp",
success: function (json) {
alert(json.d);
},
error: function () {
alert("Hit error fn!");
}
});
});
});
</script>
<input id="btn_test" type="button" value="POST" />

donc ce que je fais mal?
vous pouvez le tester vous-mêmes, c'est un service web.
Je vous remercie pour votre aide.

changer de jsonp json ne fonctionne pas en raison de la question du domaine et de la conversion de l'objet encore de retour xml

OriginalL'auteur baba-dev | 2012-01-16