diff --git a/net/ghttp/ghttp.go b/net/ghttp/ghttp.go index ddee035da..b01d27c8b 100644 --- a/net/ghttp/ghttp.go +++ b/net/ghttp/ghttp.go @@ -154,6 +154,7 @@ const ( contentTypeXml = "text/xml" contentTypeHtml = "text/html" contentTypeJson = "application/json" + contentTypeJavascript = "application/javascript" swaggerUIPackedPath = "/goframe/swaggerui" responseHeaderTraceID = "Trace-ID" responseHeaderContentLength = "Content-Length" diff --git a/net/ghttp/ghttp_response_write.go b/net/ghttp/ghttp_response_write.go index a6edbf648..1772c61a1 100644 --- a/net/ghttp/ghttp_response_write.go +++ b/net/ghttp/ghttp_response_write.go @@ -131,7 +131,7 @@ func (r *Response) WriteJsonExit(content interface{}) { // // Note that there should be a "callback" parameter in the request for JSONP format. func (r *Response) WriteJsonP(content interface{}) { - r.Header().Set("Content-Type", contentTypeJson) + r.Header().Set("Content-Type", contentTypeJavascript) // If given string/[]byte, response it directly to client. switch content.(type) { case string, []byte: diff --git a/net/ghttp/ghttp_z_unit_feature_response_test.go b/net/ghttp/ghttp_z_unit_feature_response_test.go index a563ec8d0..c0e2b565b 100644 --- a/net/ghttp/ghttp_z_unit_feature_response_test.go +++ b/net/ghttp/ghttp_z_unit_feature_response_test.go @@ -301,6 +301,8 @@ func Test_Response_Write(t *testing.T) { t.Assert(client.GetContent(ctx, "/Writefln", "name=john"), "john\n") t.Assert(client.GetContent(ctx, "/WriteJson"), "{\"name\":\"john\"}") t.Assert(client.GetContent(ctx, "/WriteJsonP"), "{\"name\":\"john\"}") + resp, _ := client.DoRequest(ctx, http.MethodGet, "/WriteJsonP", "{\"name\":\"john\"}", nil) + t.Assert(resp.Header.Get("Content-Type"), "application/javascript") t.Assert(client.GetContent(ctx, "/WriteJsonPWithStruct"), "{\"name\":\"john\"}") t.Assert(client.GetContent(ctx, "/WriteJsonPWithStruct", "callback=callback"), "callback({\"name\":\"john\"})")