从AJAX引用WCF Web服务获取没有为\ u0027System.String \ u0027

时间:2018-06-09 17:02:47

标签: javascript ajax wcf

我的网络服务如下:

[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
[WebMethod]
public string CreateRecipe(string RecipeData)
{
   //DB_9CAB52_SuzieRecipeEntities db = new DB_9CAB52_SuzieRecipeEntities();
   Suzie_RecipeAzureEntities db = new Suzie_RecipeAzureEntities();
   ObjectResult<string> result = db.CreateRecipe(RecipeData);
   return result.ToArray()[0];
}

AJAX电话:

try {
    $.ajax({
        type: "POST",
        //url: "suzierecipe.azurewebsites.net/GetRecipes",
        //url: "suzierecipe.com/suzierecipe.asmx/GetRecipes",
        url: "SuzieRecipe.asmx/CreateRecipe",
        data: '{ "RecipeData": { "SubmiterID": 1,"RecipeName": "Name","CookingInstructions": "Cook","Ingredients": "everything","KeyWords": "empty"}}',       
        contentType: "application/json; charset=utf-8",
        dataType: "jsonp",
        async: false,
        error: function (jqXHR, textStatus, errorThrown) {
            if (textStatus === "parsererror") {
                result = jqXHR.responseText;
            } else {
                alert("Error- Status: " + textStatus + " jqXHR statusText: " + jqXHR.statusText + " jqXHR Response Text:" + jqXHR.responseText);
            }
        },
        success: function (msg) {
            result = msg;
        }
    });
}
catch (err) {
    alert('This error: ' + err + 'happened.  Please try again later.');
}

错误:

  

{&#34; Message&#34;:&#34;没有为\ u0027System.String \ u0027的类型定义无参数构造函数。&#34;,&#34; StackTrace&#34;:&#34; at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal的System.Web.Script.Serialization.ObjectConverter.ConvertDictionaryToObject(IDictionary&#39; 2字典,Type类型,JavaScriptSerializer序列化程序,Boolean throwOnError,Object \ u0026 convertedObject)\ r \ n (Object o,Type type,JavaScriptSerializer serializer,Boolean throwOnError,Object \ u0026 convertedObject)\ r \ n在System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeMain(Object o,Type type,JavaScriptSerializer serializer,Boolean throwOnError,Object \ u0026在System.Web.Script.Script.Script.Script.Script.Services.WebServiceMethodData.CallMethodFromRawParams(Object target,IDictionary&#39;)中的System.Web.Script.Ser.Services.WebServiceMethodData.StrongTypeParameters(IDictionary&#39; 2 rawParams)\ r \ n的convertObject)\ r \ n System.Web.Script.Services.RestHa上的System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext上下文,WebServiceMethodData methodData,IDictionary&#39; 2 rawParams)\ r \ n中的2个参数)\ r \ n ndler.ExecuteWebServiceCall(HttpContext context,WebServiceMethodData methodData)&#34;,&#34; ExceptionType&#34;:&#34; System.MissingMethodException&#34;}

如果我将AJAX数据更改为数据:'{"RecipeData":"x"}',它可以正常工作(无论如何都会进入网络服务)。

我看了很多关于这个主题的帖子,但没有一个解决了我的问题。我尝试过使用JSON.stringify。我试过在数据周围添加双引号

{
  "RecipeData": "{
    "SubmiterID": 0,
    "RecipeName": "Name",
    "CookingInstructions": "Cook",
    "Ingredients": "everything",
    "KeyWords": "empty"
  }"
}

哪个是无效的JSON,但只是认为它不应该关心字符串中的内容。我已尝试将SubmitterID放入'{ "RecipeData": { "SubmiterID": 1}}',发生同样的错误。

0 个答案:

没有答案