简体   繁体   English

vb.net Web API $ .ajax响应不是json对象

[英]vb.net web api $.ajax response not json object

Attempting a simple web api call in vb.NET that returns json so that the jQuery ajax response is already a javascript object. 尝试在vb.NET中进行一个简单的Web api调用,该调用返回json,因此jQuery ajax响应已经是一个javascript对象。 Just can't seem to get it working. 只是似乎无法正常工作。

' GET api/<controller>
Public Function GetValues()
    Dim json As New Dictionary(Of String, String)
    json.Add("status", "success")
    json.Add("msg", "good job")

    Dim serializer = New JavaScriptSerializer()
    Return serializer.Serialize(json)

    'Return JsonConvert.SerializeObject(json)
End Function

Ajax call: Ajax呼叫:

$.ajax({
    url: "api/products",
    type: 'GET',
    contentType: "application/json; charset=utf-8",
    data: JSON.stringify({key:'key'}),
    dataType: 'json',
    success: function (response) {
        console.log(response);  // prints {"status":"success","msg":"good job"}
        console.log(response.status);  // prints undefined
    }
});

Chrome shows: Chrome显示:
Request header: Content-Type:application/json; 请求标头:Content-Type:application / json; charset=utf-8 字符集= utf-8
Response header: Content-Type:application/json; 响应头:Content-Type:application / json; charset=utf-8 字符集= utf-8
Network/Response tab shows: "{\\"status\\":\\"success\\",\\"msg\\":\\"good job\\"}" 网络/响应选项卡显示:“ {\\”状态\\“:\\”成功\\“,\\” msg \\“:\\”好工作\\“}”

It's obviously not being turned into a javascript object automatically but I can't figure out why. 显然,它不会自动变成javascript对象,但是我不知道为什么。 Doesn't matter if I return the response from JavaScriptSerialize.Serialize() or NewtonSofts JsonConvert.SerializeObject. 我返回JavaScriptSerialize.Serialize()还是NewtonSofts JsonConvert.SerializeObject的响应都没关系。

I must be missing something fundamental here. 我一定在这里缺少一些基本的东西。 Can anyone point me in the right direction? 谁能指出我正确的方向?

So I figured out my bonehead move. 所以我想出了我的举动。 The function that was returning what I was serializing to json as a string until I told it otherwise. 该函数以字符串形式返回我序列化到json的内容,直到我另行告知为止。 I had abandoned VB and switched to C# where it was failing too. 我放弃了VB并切换到C#失败的地方。 In the end I changed the C# versrion from: 最后,我将C#版本从以下位置更改:
public string UploadFile()
to
public Dictionary<string, string> UploadFile()

And it would be similar in VB something like 在VB中类似
Public Function GetValues() As String
to
Public Function GetValues() As Dictionary(Of String, String)

Then I got a javascript object without having to use something like JSON.parse(). 然后我得到了一个javascript对象,而不必使用JSON.parse()之类的东西。 :-) :-)

Please try below code. 请尝试以下代码。 I have converted your JSON response to javascript object. 我已经将您的JSON响应转换为javascript对象。

$.ajax({
    url: "api/products",
    type: 'GET',
    contentType: "application/json; charset=utf-8",
    data: JSON.stringify({key:'key'}),
    dataType: 'json',
    success: function (response) {
    var objResponse = JSON.parse(response);

        console.log(response);  // prints {"status":"success","msg":"good job"}
        console.log(objResponse.status);  // prints undefined
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM