Javascript 返回奇怪响应的jQuery ajax()函数

Javascript 返回奇怪响应的jQuery ajax()函数,javascript,jquery,ajax,Javascript,Jquery,Ajax,我正在使用jQueryajax()函数进行ajax调用。 我不断得到错误: SyntaxError:意外标记,该错误显示您有语法错误。这不是AJAX响应,在代码的某些地方,您遇到了一些问题。如果您可以创建一个JSFIDLE,我们可以帮助您确定问题所在。您已经告诉jQuery需要JSON,但它不是JSON。还要注意,contentType是请求的MIME类型,而dataType告诉jQuery您希望响应是什么样子。因此,您设置HTTP请求的方式告诉服务器您正在发送JSON,但实际上并没有这样做。好

我正在使用jQuery
ajax()
函数进行ajax调用。 我不断得到错误:


SyntaxError:意外标记,该错误显示您有语法错误。这不是AJAX响应,在代码的某些地方,您遇到了一些问题。如果您可以创建一个JSFIDLE,我们可以帮助您确定问题所在。您已经告诉jQuery需要JSON,但它不是JSON。还要注意,
contentType
是请求的MIME类型,而
dataType
告诉jQuery您希望响应是什么样子。因此,您设置HTTP请求的方式告诉服务器您正在发送JSON,但实际上并没有这样做。好吧,这看起来不像JSON,是吗?这是一条错误消息,告诉您服务器端代码中存在错误。您使用的是什么
php
框架?
function testAjax(subject)
{    
$.ajax({
        url: '/profile/testAjax',
        type: 'GET',
        data: { field1: subject} ,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
        //your success code
        alert(response);
        },
        error: function (xhr, ajaxOptions, thrownError) {
        alert("Error: " + thrownError);
     }
    }); 
}
public function testAjax($subject)
{
    return "This works:" . $subject;
}
$(document).on('click', '#applyButton', function(e) 
{
e.preventDefault();
testAjax($(this).val()) 
});
GET write

200 OK

localhost

11.0 KB

127.0.0.1:80


83ms
GET jquery.min.js

200 OK

localhost

147 B

127.0.0.1:80


80ms
GET icons.woff2

200 OK

localhost

147 B

127.0.0.1:80


78ms
GET ajax?field1=Boogie

200 OK

localhost

3.1 KB

127.0.0.1:80


84ms
ParamsHeadersResponseHTMLCacheCookies

<br />
<font size='1'><table class='xdebug-error xe-warning' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Warning: Missing argument 1 for profileController::ajax(), called in C:\wamp\www\application\router.class.php on line 85 and defined in C:\wamp\www\controller\profileController.php on line <i>440</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0006</td><td bgcolor='#eeeeec' align='right'>254992</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp\www\index.php' bgcolor='#eeeeec'>..\index.php<b>:</b>0</td></tr>
<tr><td bgcolor='#eeeeec' align='center'>2</td><td bgcolor='#eeeeec' align='center'>0.0735</td><td bgcolor='#eeeeec' align='right'>2684528</td><td bgcolor='#eeeeec'>router->loader(  )</td><td title='C:\wamp\www\index.php' bgcolor='#eeeeec'>..\index.php<b>:</b>34</td></tr>
<tr><td bgcolor='#eeeeec' align='center'>3</td><td bgcolor='#eeeeec' align='center'>0.0755</td><td bgcolor='#eeeeec' align='right'>2766056</td><td bgcolor='#eeeeec'>profileController->ajax(  )</td><td title='C:\wamp\www\application\router.class.php' bgcolor='#eeeeec'>..\router.class.php<b>:</b>85</td></tr>
</table></font>
<br />
<font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Notice: Undefined variable: subject in C:\wamp\www\controller\profileController.php on line <i>442</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0006</td><td bgcolor='#eeeeec' align='right'>254992</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp\www\index.php' bgcolor='#eeeeec'>..\index.php<b>:</b>0</td></tr>
<tr><td bgcolor='#eeeeec' align='center'>2</td><td bgcolor='#eeeeec' align='center'>0.0735</td><td bgcolor='#eeeeec' align='right'>2684528</td><td bgcolor='#eeeeec'>router->loader(  )</td><td title='C:\wamp\www\index.php' bgcolor='#eeeeec'>..\index.php<b>:</b>34</td></tr>
<tr><td bgcolor='#eeeeec' align='center'>3</td><td bgcolor='#eeeeec' align='center'>0.0755</td><td bgcolor='#eeeeec' align='right'>2766056</td><td bgcolor='#eeeeec'>profileController->ajax(  )</td><td title='C:\wamp\www\application\router.class.php' bgcolor='#eeeeec'>..\router.class.php<b>:</b>85</td></tr>
</table></font>