Why am I having to set the dataType to text?
I have the following script which works, but I don't understand why it will not work when the type is set to json:
Serverside:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Response.Clear()
Response.ContentType = "text/plain" '//#### <- should't this be text/json? ###
Response.Write(getTabFrame())
Response.End()
End Sub
Function getTabFrame() As String
objStringBuilder = New StringBuilder()
objStringBuilder.Append("[")
objStringBuilder.Append("{""key"":1,""value"":""Default""},")
objStringBuilder.Append("{""key"":2,""value"":""Reports""},")
objStringBuilder.Append("{""key"":3,""val开发者_开发知识库ue"":""Other""}")
objStringBuilder.Append("]")
Return objStringBuilder.ToString
End Function
Clientside:
$.ajax({
url: 'serverside',
type: 'GET',
dataType: 'text', //#### <------------------------ shouldn't this be json? ###
error: function(xhr, status, error) {
alert('Error: ' + status + '\nError Text: ' + error + '\nResponse Text: ' + xhr.responseText);
},
success: function(results) {
var obj = jQuery.parseJSON(results);
var len = obj.length;
for(i=0; i<len; i++) {
$("#tabs").tabs("add","tabs.aspx?tab=" + obj[i].key, obj[i].value)
};
}
});
When I change those values to json, the whole thing stops working and returns "null"...
Why?
if dataType
is json
, you don't need to do parseJSON
on results
- it will already be a javascript object literal.
Try using Response.ContentType = "application/json";
on server side, and in AJAX call use
contentType: "application/json; charset=utf-8",
dataType: "json",
I think it should be an appropriate to use • Response.ContentType = "text/html";
The XML validates and checks the markup validity of Web documents in HTML, XHTML, SMIL, MathML, etc. to conform to w3 standards.
SAMPLE CODE: (With error)
protected void Page_Load(object sender, EventArgs e)
{
Response.Clear();
Response.ContentType = "text/json"; //#### <- should't this be ‘application/json’?
Response.Write(getTabFrame());
Response.End();
}
private string getTabFrame()
{
var objStringBuilder = new StringBuilder();
objStringBuilder.Append("[");
objStringBuilder.Append("{\"key\":1,\"value\":\"Default\"},");
objStringBuilder.Append("{\"key\":2,\"value\":\"Reports\"},") ;
objStringBuilder.Append("{\"key\":3,\"value\":\"Other\"}");
objStringBuilder.Append("]");
return objStringBuilder.ToString();
}
Page Error Output:
The XML page cannot be displayed Cannot view XML input using style sheet. Please correct the error and then click the Refresh button, or try again later.
Invalid at the top level of the document. Error processing resource 'http://localhost:1713/Default.aspx'. Line 1, Position...
[{"key":1,"value":"Default"},{"key":2,"value":"Reports"},{"key":3,"value":"Other"}] ^
SAMPLE CODE: (No error)
protected void Page_Load(object sender, EventArgs e)
{
Response.Clear();
Response.ContentType = "text/html"; //#### <- should't this be text/html? ###
Response.Write(getTabFrame());
Response.End();
}
private string getTabFrame()
{
var objStringBuilder = new StringBuilder();
objStringBuilder.Append("[");
objStringBuilder.Append("{\"key\":1,\"value\":\"Default\"},");
objStringBuilder.Append("{\"key\":2,\"value\":\"Reports\"},") ;
objStringBuilder.Append("{\"key\":3,\"value\":\"Other\"}");
objStringBuilder.Append("]");
return objStringBuilder.ToString();
}
Page Output(No error):
[{"key":1,"value":"Default"},{"key":2,"value":"Reports"},{"key":3,"value":"Other"}]
精彩评论